php 發(fā)送帶附件郵件示例
更新時間:2014年01月23日 17:39:28 作者:
php發(fā)送郵件,在網(wǎng)上會很多的相關(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ā)送郵件實例分析)
- php郵件發(fā)送的兩種方式
- 功能齊全的PHP發(fā)送郵件類代碼附詳細(xì)說明
- php郵件發(fā)送,php發(fā)送郵件的類
- PHPMailer郵件類利用smtp.163.com發(fā)送郵件方法
- phpmailer在服務(wù)器上不能正常發(fā)送郵件的解決辦法
- php中mail函數(shù)發(fā)送郵件失敗的解決方法
- phpmailer簡單發(fā)送郵件的方法(附phpmailer源碼下載)
- 使用php發(fā)送有附件的電子郵件-(PHPMailer使用的實例分析)
- ThinkPHP5郵件發(fā)送服務(wù)封裝(可發(fā)附件)
相關(guān)文章
Laravel 5.5基于內(nèi)置的Auth模塊實現(xiàn)前后臺登陸詳解
最近在使用laravel5.5,利用其實現(xiàn)了一個功能,下面分享給大家,這篇文章主要給大家介紹了關(guān)于Laravel 5.5基于內(nèi)置的Auth模塊如何實現(xiàn)前后臺登陸的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
PHP實現(xiàn)電商訂單自動確認(rèn)收貨redis隊列
下面小編就為大家?guī)硪黄狿HP實現(xiàn)電商訂單自動確認(rèn)收貨redis隊列。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05
Codeigniter中禁止A Database Error Occurred錯誤提示的方法
在默認(rèn)的情況下,CodeIgniter會顯示所有的PHP錯誤。但是當(dāng)你開發(fā)程序結(jié)束時,你可能想要改變這個情況。這篇文章主要介紹了Codeigniter中禁止A Database Error Occurred錯誤提示的方法,需要的朋友可以參考下2014-06-06
PHP+Mysql+Ajax+JS實現(xiàn)省市區(qū)三級聯(lián)動
最近做了個項目,需要用到省市區(qū)三級聯(lián)動,上網(wǎng)翻了不少資料,于是有了下面的思路和代碼2014-05-05

