php實(shí)現(xià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);
} www.dhdzp.com
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類(lèi)型
*/
?>
演示示例
<?php
require_once('emailclass.php');
//發(fā)送郵件
//主題
$subject = "test send email";
//收件人
$sendto = 'abc@163.com';
//發(fā)件人
$replyto = 'cdf@163.com';
//內(nèi)容 www.dhdzp.com
$message = "test send email content";
//附件
$filename = 'test.jpg';
//附件類(lèi)別
$mimetype = "image/jpeg";
$mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filename,$excelname,$mimetype);
$mailfile->sendfile();
?>
- 功能齊全的PHP發(fā)送郵件類(lèi)代碼附詳細(xì)說(shuō)明
- PHPMailer郵件類(lèi)利用smtp.163.com發(fā)送郵件方法
- php郵件發(fā)送,php發(fā)送郵件的類(lèi)
- PHPMailer使用教程(PHPMailer發(fā)送郵件實(shí)例分析)
- php 發(fā)送帶附件郵件示例
- php使用smtp發(fā)送支持附件的郵件示例
- PHPMailer發(fā)送HTML內(nèi)容、帶附件的郵件實(shí)例
- thinkphp實(shí)現(xiàn)發(fā)送郵件密碼找回功能實(shí)例
- php中mail函數(shù)發(fā)送郵件失敗的解決方法
- php實(shí)現(xiàn)在站點(diǎn)里面添加郵件發(fā)送的功能
相關(guān)文章
php resizeimage 部分jpg文件 生成縮略圖失敗的原因分析及解決辦法
這篇文章主要介紹了php resizeimage 部分jpg文件 生成縮略圖失敗的原因分析及解決辦法的相關(guān)資料,需要的朋友可以參考下2016-03-03
PHP用函數(shù)嵌入網(wǎng)站訪問(wèn)量計(jì)數(shù)器
這篇文章主要為大家詳細(xì)介紹了PHP用函數(shù)嵌入網(wǎng)站訪問(wèn)量計(jì)數(shù)器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
php從數(shù)據(jù)庫(kù)中讀取特定的行(實(shí)例)
下面小編就為大家?guī)?lái)一篇php從數(shù)據(jù)庫(kù)中讀取特定的行(實(shí)例)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06
thinkPHP框架動(dòng)態(tài)配置用法實(shí)例分析
這篇文章主要介紹了thinkPHP框架動(dòng)態(tài)配置用法,結(jié)合實(shí)例形式分析了thinkPHP3.0之前的動(dòng)態(tài)配置操作技巧及緩存相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2018-06-06
基于PHP+jQuery+MySql實(shí)現(xiàn)紅藍(lán)(頂踩)投票代碼
當(dāng)雙方意見(jiàn)不統(tǒng)一的情況下,需要用投票達(dá)成協(xié)議,本文通過(guò)實(shí)例給大家介紹基于PHP+jQuery+MySql實(shí)現(xiàn)紅藍(lán)(頂踩)投票代碼,需要的朋友可以參考下2015-08-08
PHP實(shí)現(xiàn)單條sql執(zhí)行多個(gè)數(shù)據(jù)的insert語(yǔ)句方法
今天小編就為大家分享一篇PHP實(shí)現(xiàn)單條sql執(zhí)行多個(gè)數(shù)據(jù)的insert語(yǔ)句方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10

