php 發(fā)送帶附件郵件示例
更新時(shí)間:2014年01月23日 17:39:28 作者:
php發(fā)送郵件,在網(wǎng)上會(huì)很多的相關(guān)文章,而本文為大家介紹的是發(fā)送帶附件郵件,不了解的朋友可以參考下
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 -- 回復(fù)地址
$message -- 信件內(nèi)容
$filename -- 附件文件名
$downfilename -- 下載的文件名
$mimetype -- mime類型
*/
?>
Demo
<?php
require_once('emailclass.php');
//發(fā)送郵件
//主題
$subject = "test send email";
//收件人
$sendto = 'abc@163.com';
//發(fā)件人
$replyto = 'cdf@163.com';
//內(nèi)容
$message = "test send email content";
//附件
$filename = 'test.jpg';
//附件類別
$mimetype = "image/jpeg";
$mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filename,$excelname,$mimetype);
$mailfile->sendfile();
?>
復(fù)制代碼 代碼如下:
<?
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 -- 回復(fù)地址
$message -- 信件內(nèi)容
$filename -- 附件文件名
$downfilename -- 下載的文件名
$mimetype -- mime類型
*/
?>
Demo
復(fù)制代碼 代碼如下:
<?php
require_once('emailclass.php');
//發(fā)送郵件
//主題
$subject = "test send email";
//收件人
$sendto = 'abc@163.com';
//發(fā)件人
$replyto = 'cdf@163.com';
//內(nèi)容
$message = "test send email content";
//附件
$filename = 'test.jpg';
//附件類別
$mimetype = "image/jpeg";
$mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filename,$excelname,$mimetype);
$mailfile->sendfile();
?>
您可能感興趣的文章:
- PHPMailer使用教程(PHPMailer發(fā)送郵件實(shí)例分析)
- php郵件發(fā)送的兩種方式
- 功能齊全的PHP發(fā)送郵件類代碼附詳細(xì)說(shuō)明
- php郵件發(fā)送,php發(fā)送郵件的類
- PHPMailer郵件類利用smtp.163.com發(fā)送郵件方法
- phpmailer在服務(wù)器上不能正常發(fā)送郵件的解決辦法
- php中mail函數(shù)發(fā)送郵件失敗的解決方法
- phpmailer簡(jiǎn)單發(fā)送郵件的方法(附phpmailer源碼下載)
- 使用php發(fā)送有附件的電子郵件-(PHPMailer使用的實(shí)例分析)
- ThinkPHP5郵件發(fā)送服務(wù)封裝(可發(fā)附件)
相關(guān)文章
Smarty模板變量與調(diào)節(jié)器實(shí)例詳解
這篇文章主要介紹了Smarty模板變量與調(diào)節(jié)器,結(jié)合實(shí)例形式詳細(xì)分析了Smarty模板變量與調(diào)節(jié)器基本概念、分類、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-07-07
Laravel 5.5基于內(nèi)置的Auth模塊實(shí)現(xiàn)前后臺(tái)登陸詳解
最近在使用laravel5.5,利用其實(shí)現(xiàn)了一個(gè)功能,下面分享給大家,這篇文章主要給大家介紹了關(guān)于Laravel 5.5基于內(nèi)置的Auth模塊如何實(shí)現(xiàn)前后臺(tái)登陸的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
Laravel如何實(shí)現(xiàn)自動(dòng)加載類
今天小編就為大家整理了一篇Laravel如何實(shí)現(xiàn)自動(dòng)加載類的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10
PHP實(shí)現(xiàn)電商訂單自動(dòng)確認(rèn)收貨redis隊(duì)列
下面小編就為大家?guī)?lái)一篇PHP實(shí)現(xiàn)電商訂單自動(dòng)確認(rèn)收貨redis隊(duì)列。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
Codeigniter中禁止A Database Error Occurred錯(cuò)誤提示的方法
在默認(rèn)的情況下,CodeIgniter會(huì)顯示所有的PHP錯(cuò)誤。但是當(dāng)你開(kāi)發(fā)程序結(jié)束時(shí),你可能想要改變這個(gè)情況。這篇文章主要介紹了Codeigniter中禁止A Database Error Occurred錯(cuò)誤提示的方法,需要的朋友可以參考下2014-06-06
magento后臺(tái)無(wú)法登錄解決辦法的兩種方法
可能很多朋友有同樣的經(jīng)歷,magento在服務(wù)器中配置域名是可以正常的訪問(wèn)了,但是在本地配置后卻無(wú)法登錄后臺(tái),賬號(hào)密碼登錄的時(shí)候發(fā)現(xiàn)出現(xiàn)空白,無(wú)法跳轉(zhuǎn)到后臺(tái),本文章向大家介紹兩種解決本地magento后臺(tái)無(wú)法登錄的方法,需要的朋友可以參考下2016-12-12
PHP+Mysql+Ajax+JS實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)
最近做了個(gè)項(xiàng)目,需要用到省市區(qū)三級(jí)聯(lián)動(dòng),上網(wǎng)翻了不少資料,于是有了下面的思路和代碼2014-05-05

