php 发送带附件邮件示例_php实例

emailclass.php

复制代码 代码如下:

<?
class CMailFile {

var $subject;
var $addr_to;
var $text_body;
var $text_encoded;
var $mime_headers;
var $mime_boundary = "--==================_846811060==_";
var $smtp_headers;

function CMailFile($subject,$to,$from,$msg,$filename,$downfilename,$mimetype = "application/octet-stream",$mime_filename = false) {
$this->subject = $subject;
$this->addr_to = $to;
$this->smtp_headers = $this->write_smtpheaders($from);
$this->text_body = $this->write_body($msg);
$this->text_encoded = $this->attach_file($filename,$downfilename,$mimetype,$mime_filename);
$this->mime_headers = $this->write_mimeheaders($filename, $mime_filename);
}

function attach_file($filename,$downfilename,$mimetype,$mime_filename) {
$encoded = $this->encode_file($filename);
if ($mime_filename) $filename = $mime_filename;
$out = "--" . $this->mime_boundary . "\n";
$out = $out . "Content-type: " . $mimetype . "; name=\"$filename\";\n";
$out = $out . "Content-Transfer-Encoding: base64\n";
$out = $out . "Content-disposition: attachment; filename=\"$downfilename\"\n\n";
$out = $out . $encoded . "\n";
$out = $out . "--" . $this->mime_boundary . "--" . "\n";
return $out;
}

function encode_file($sourcefile) {
if (is_readable($sourcefile)) {
$fd = fopen($sourcefile, "r");
$contents = fread($fd, filesize($sourcefile));
$encoded = chunk_split(base64_encode($contents));
fclose($fd);
}
return $encoded;
}

function sendfile() {
$headers = $this->smtp_headers . $this->mime_headers;
$message = $this->text_body . $this->text_encoded;
mail($this->addr_to,$this->subject,$message,$headers);
}

function write_body($msgtext) {
$out = "--" . $this->mime_boundary . "\n";
$out = $out . "Content-Type: text/plain; charset=\"us-ascii\"\n\n";
$out = $out . $msgtext . "\n";
return $out;
}

function write_mimeheaders($filename, $mime_filename) {
if ($mime_filename) $filename = $mime_filename;
$out = "MIME-version: 1.0\n";
$out = $out . "Content-type: multipart/mixed; ";
$out = $out . "boundary=\"$this->mime_boundary\"\n";
$out = $out . "Content-transfer-encoding: 7BIT\n";
$out = $out . "X-attachments: $filename;\n\n";
return $out;
}

function write_smtpheaders($addr_from) {
$out = "From: $addr_from\n";
$out = $out . "Reply-To: $addr_from\n";
$out = $out . "X-Mailer: PHP3\n";
$out = $out . "X-Sender: $addr_from\n";
return $out;
}
}

/*用法 - 例如:mimetype 为 "image/gif"
$mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filename,$mimetype);
$mailfile->sendfile();

$subject -- 主题
$sendto -- 收信人地址
$replyto -- 回复地址
$message -- 信件内容
$filename -- 附件文件名
$downfilename -- 下載的文件名
$mimetype -- mime类型
*/
?>

Demo

复制代码 代码如下:

<?php
require_once('emailclass.php');

//发送邮件

//主題
$subject = "test send email";

//收件人
$sendto = 'abc@163.com';

//發件人
$replyto = 'cdf@163.com';

//內容
$message = "test send email content";

//附件
$filename = 'test.jpg';

//附件類別
$mimetype = "image/jpeg";

$mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filename,$excelname,$mimetype);
$mailfile->sendfile();
?>

时间: 2024-11-02 04:47:38

php 发送带附件邮件示例_php实例的相关文章

php 发送带附件邮件示例

 php发送邮件,在网上会很多的相关文章,而本文为大家介绍的是发送带附件邮件,不了解的朋友可以参考下 emailclass.php   代码如下: <?  class CMailFile {    var $subject;  var $addr_to;  var $text_body;  var $text_encoded;  var $mime_headers;  var $mime_boundary = "--==================_846811060==_"

php实现的发送带附件邮件类实例_php技巧

本文实例讲述了php实现的发送带附件邮件类的方法,是一个非常实用的功能.分享给大家供大家参考.具体方法如下: emailclass.php类文件如下: <? class CMailFile { var $subject; var $addr_to; var $text_body; var $text_encoded; var $mime_headers; var $mime_boundary = "--==================_846811060==_"; var $

JavaMail 发送带附件邮件遇到的问题 !

问题描述 大家好,我刚开始接触javamail,遇到一个小问题请大家帮忙解决下.我在发送带附件邮件的时候附件是可以发过去的,但是不管是中文名的还是英文名的附件到了接收邮件的那边都会是带地址的一串名字,例如:CUsersxinge1023Desktop___,_c.txt或CUsersxinge1023Desktop__茆_.jpg乱码问题我能搞定,但是就是搞不通为什么名字是带地址的,请大家帮忙看下,我只想要文件名,而不要前面的那一长串文件路径,我的电脑系统是win7,服务器是tomcat的,谢谢

php使用smtp发送支持附件的邮件示例_php实例

轻量级PHP邮件发送,需要有smtp服务器,代码经过多次实战使用,现在把代码分享给大家 复制代码 代码如下: <?php/*邮件发送smtp服务联结smtp服务器,进行邮件发送,版权所有,不能复制@author:jackbrown;@qq: 610269963 @time:2011-8-20;@version:1.0.3;*/class smtp{  /*邮件用户名*/ public $mailUser = MAIL_USER;  /*邮件密码*/ public $mailPwd = MAIL_

发送带附件邮件成功,我用新浪测试成功为什么下载附件以后,打开里边什么都没有啊???

问题描述 核心代码难道是新浪的问题?大家帮我看看publicboolSendMail(MailInfomailinfo){string[]mailTos=mailinfo.MMailTo;string[]mailCcs=mailinfo.MMailCc;string[]mailBccs=mailinfo.MMailBcc;string[]attachments={"E:\data.doc"};boolisSend=false;MailMessageEmail=newMailMessag

Codeigniter实现发送带附件的邮件_php实例

本文实例讲述了Codeigniter实现发送带附件的邮件的方法.分享给大家供大家参考.具体分析如下: attach() 方法允许你的发邮件时带上附件,下面是演示代码 复制代码 代码如下: $this->load->library('email'); $this->email->from('w3@w3mentor.com', 'W3M'); $this->email->subject('Email Test with attachment'); $this->ema

php简单实现发送带附件的邮件_php技巧

本文实例讲述了php简单实现发送带附件的邮件.分享给大家供大家参考.具体如下: 下面是静态html代码: <html> <head> <title>带附件的邮件发送</title> </head> <body> <form method="post" name="form1" action="sendmail.php" ENCTYPE="multipart/f

发送带附件的HTML格式邮件例程可以带附件

如何通过 ASP 来发送带附件.格式为 HTML 的邮件?下面提供了一个例程. <%@ LANGUAGE=VBScript%><%Response.Buffer = TrueResponse.Expires = 0 '创建对象实例Set myMail = Server.CreateObject("CDONTS.NewMail") '以下是将要发送的内容HTML = "<html>"HTML = HTML & "<

发送带附件的HTML格式邮件例程

发送带附件的HTML格式邮件例程 如何通过 ASP 来发送带附件.格式为 HTML 的邮件?下面提供了一个例程. <%@ LANGUAGE=VBScript%><%Response.Buffer = TrueResponse.Expires = 0 '创建对象实例Set myMail = Server.CreateObject("CDONTS.NewMail") '以下是将要发送的内容HTML = "<html>"HTML = HTML