利用php中mail函數(shù)發(fā)送帶有附件的郵件
來源:程序員人生 發(fā)布時(shí)間:2014-06-02 22:00:39 閱讀次數(shù):4426次
mail函數(shù),發(fā)送郵件
語法: mail(to,subject,message,headers,parameters)
to 規(guī)定郵件的接收者
subject 規(guī)定郵件的主題。該參數(shù)不能包含任何換行字符
message 規(guī)定要發(fā)送的消息
headers 規(guī)定額外的報(bào)頭,比如 From, Cc 以及 Bcc
parameters 規(guī)定 sendmail 程序的額外參數(shù)。
碰到的主要問題是亂碼問題,剛開始是某些客戶端接收郵件時(shí)好(比如QQ郵箱,估計(jì)帶自動(dòng)那個(gè)識(shí)別編碼)的有些不foxmail、ipad顯示亂碼,解決方式正確的設(shè)置這個(gè)mail的headers就行了,下面是我使用的完美的無亂碼的例子。
在PHP中配置php.ini文件過程分為兩個(gè)步驟:
1.先找到你放置所有PHP,Apache,MySQL文件的地方,在PHP文件夾里你可以發(fā)現(xiàn)有一個(gè)文件:php.ini,打開后,找到mail function地方,將原來的配置代碼改為如下(僅對(duì)windows系統(tǒng)):
- [mail function]
- ; For Win32 only.
- SMTP =smtp.sohu.com
- mtp_port=25
- ; For Win32 only.
- sendmail_from = 填上你的電子郵件全稱。
此處為以sohu的郵件服務(wù)器設(shè)置,如果你用163的郵箱,則設(shè)置為:smtp.163.com
2.在C盤搜索php.ini,選擇不是快捷方式的那一個(gè)php.ini,應(yīng)該在C/WINDOWS里面的,打開它,如上面一樣修改它,保存,設(shè)置完后,記得重啟Apache服務(wù)器,然后mail()函數(shù)就可以用了,示例代碼如下:
- <?php
-
- $headers = "MIME-Version: 1.0" . "rn";
- $headers .= "Content-type:text/html; charset=utf-8";
- mail($to,$subject,$message,$headers);
- ?>
上面函數(shù)不可以帶附件了,下面我們升級(jí)一下,代碼如下:
- <?php
- class Mail {
- private $topic;
- private $toaddr;
- private $fromaddr;
- private $cc;
- private $content;
- private $attach;
- private $header;
- private $domain;
- private $msg;
- private $filename;
- private $filemime;
- private $filestr;
- private $boundary;
- private $uniqid;
- private $eol;
-
- function __construct(){
- $this->getEOT();
- $this->getUniq_id();
- $this->header='';
- $this->attach='';
- $this->cc='';
- $this->msg='';
-
- }
-
- public function getFromaddr() {
- return $this->fromaddr;
- }
- public function setFromaddr($fromaddr) {
- $this->fromaddr = $fromaddr;
- }
- public function getTopic() {
- return $this->topic;
- }
-
- public function getToaddr() {
- return $this->toaddr;
- }
-
- public function getCc() {
- return $this->cc;
- }
- public function getContent() {
- return $this->content;
- }
-
- public function getAttach() {
- return $this->attach;
- }
-
- public function setTopic($topic) {
- $this->topic = mb_convert_encoding(trim($topic),'UTF-8','auto');
- }
- public function setToaddr($toaddr) {
- $this->toaddr = trim($toaddr);
- }
-
- public function setCc($cc) {
- $this->cc = trim($cc);
- }
-
- public function setContent($content) {
- $this->content = mb_convert_encoding(trim($content),'UTF-8','auto');
- }
-
- public function setAttach($attach) {
- $this->attach = trim($attach);
- }
- public function getDomain() {
- return $this->domain;
- }
- public function setDomain($domain) {
- $this->domain = $domain;
- }
-
-
-
-
- private function getEOT() {
- if (strtoupper ( substr ( PHP_OS, 0, 3 ) == 'WIN' )) {
- $this->eol = "rn";
- } elseif (strtoupper ( substr ( PHP_OS, 0, 3 ) == 'MAC' )) {
- $this->eol= "r";
- } else {
- $this->eol = "n";
- }
- }
-
- private function getBoundary(){
- $this->boundary= '--'.substr(md5(time().rand(1000,2000)),0,16);
- }
- private function getUniq_id(){
- $this->uniqid= md5(microtime().time().rand(1,100));
- }
- private function outputCommonHeader(){
- $this->header .= 'From: '.$this->fromaddr.$this->eol;
-
-
- $this->header .= 'Message-ID: <'.$this->uniqid.$this->domain.'>'.$this->eol;
- $this->header .= 'MIME-Version: 1.0'.$this->eol;
- $this->header .= 'Reply-To: '.$this->fromaddr.$this->eol;
- $this->header .= 'Return-Path: '.$this->fromaddr.$this->eol;
- $this->header .= 'X-Mailer: Xmail System'.$this->eol;
- $this->header .= 'Content-Disposition: inline'.$this->eol;
- }
- private function mime_content_type ( $f )
- {
- $temp = trim ( exec ('file -bi ' . escapeshellarg ( $f ) ) ) ;
- $temp = preg_replace('/s+/',' ',$temp);
- $temp = explode(' ',$temp);
- return $temp[0];
- }
-
-
-
-
- private function mailWithCC(){
- $this->header .= 'Cc: '.$this->cc.$this->eol;
- $this->header .= 'Content-type: text/html; charset=UTF-8'.$this->eol;
- $this->header .= 'Content-Transfer-Encoding: 8bit'.$this->eol;
- $this->msg = $this->content;
- if(mail($this->toaddr,$this->topic,$this->msg,$this->header)){
- return 1;
- }else{
- return 0;
- }
- }
-
-
-
- private function attachmentToBase64($filedir){
- $this->filename = basename($filedir);
- @$fopen = fopen($filedir,'r');
- $str = fread($fopen,filesize($filedir));
- $str = base64_encode($str);
- $this->filestr = $str;
- }
-
-
-
-
- private function mailWithAttach(){
- $this->attachmentToBase64($this->attach);
- $this->header .= 'Content-type: multipart/mixed; boundary="'.str_replace('--','',$this->boundary).'"'.$this->eol;
- $this->msg .= $this->eol.$this->boundary.$this->eol;
- $this->msg .= 'Content-Type: text/html; charset=utf-8'.$this->eol;
- $this->msg .= 'Content-Disposition: inline'.$this->eol;
- $this->msg .= $this->eol.$this->content.$this->eol;
- $this->msg .= $this->boundary.$this->eol;
- $this->msg .= 'Content-Type: '.$this->mime_content_type($this->attach).$this->eol;
- $this->msg .= 'Content-Disposition: attachment; filename="'.$this->filename.'"'.$this->eol;
- $this->msg .= 'Content-Transfer-Encoding: base64'.$this->eol;
- $this->msg .= $this->eol.$this->filestr.$this->eol;
- $this->msg .= $this->eol.$this->boundary.'--';
- if(mail($this->toaddr,$this->topic,$this->msg,$this->header)){
- return 1;
- }else{
- return 0;
- }
- }
-
-
-
- private function mailAll(){
- $this->attachmentToBase64($this->attach);
- $this->header .= 'Cc: '.$this->cc.$this->eol;
- $this->header .= 'Content-type: multipart/mixed; boundary="'.str_replace('--','',$this->boundary).'"'.$this->eol;
- $this->msg .= $this->eol.$this->boundary.$this->eol;
- $this->msg .= 'Content-Type: text/html; charset=utf-8'.$this->eol;
- $this->msg .= 'Content-Disposition: inline'.$this->eol;
- $this->msg .= $this->eol.$this->content.$this->eol;
- $this->msg .= $this->boundary.$this->eol;
- $this->msg .= 'Content-Type: '.$this->mime_content_type($this->attach).$this->eol;
- $this->msg .= 'Content-Disposition: attachment; filename="'.$this->filename.'"'.$this->eol;
- $this->msg .= 'Content-Transfer-Encoding: base64'.$this->eol;
- $this->msg .= $this->eol.$this->filestr.$this->eol;
- $this->msg .= $this->eol.$this->boundary.'--';
- if(mail($this->toaddr,$this->topic,$this->msg,$this->header)){
- return 1;
- }else{
- return 0;
- }
- }
-
-
-
- private function mailSimple(){
- $this->header .= 'Content-type: text/html; charset=UTF-8'.$this->eol;
- $this->header .= 'Content-Transfer-Encoding: 8bit'.$this->eol;
- $this->msg = $this->content;
- if(mail($this->toaddr,$this->topic,$this->msg,$this->header)){
- return 1;
- }else{
- return 0;
- }
- }
- public function send(){
- if(emptyempty($this->attach)&&emptyempty($this->cc)){
- $this->outputCommonHeader();
- return $this->mailSimple();
- }else if(emptyempty($this->attach)){
- $this->outputCommonHeader();
- return $this->mailWithCC();
- }else if(emptyempty($this->cc)){
- $this->outputCommonHeader();
- $this->getBoundary();
- return $this->mailWithAttach();
- }else if(!emptyempty($this->toaddr)&&!emptyempty($this->topic)&&!emptyempty($this->cc)&&!emptyempty($this->content)&&!emptyempty($this->attach)){
- $this->outputCommonHeader();
- $this->getBoundary();
- return $this->mailAll();
- }
- }
- }
示例代碼,有些變量需要上下文環(huán)境,代碼如下:
- $m = new Mail();
- $m->setToaddr($this->temp['receipt_address']);
- $m->setTopic($this->temp['mail_title']);
- $m->setContent($this->temp['mail_content']);
- $m->setFromaddr($_SESSION['user']['name'].' <'.$_SESSION['user']['name'].'@'.SystemDomain.'>');
- $m->setDomain('@'.SystemDomain);
- $m->setCc($this->temp['cc_address']);
- $m->setAttach(PATH.'/temp/'.$this->temp['attachment_file']);
- $m->send();
優(yōu)點(diǎn):使用方便就一個(gè)簡(jiǎn)單的函數(shù)
缺點(diǎn):需要php.ini支持該函數(shù),如果某些服務(wù)器不支持而又不能改環(huán)境那就不行了而且總是不穩(wěn)定,發(fā)的有時(shí)能收到有時(shí)不能.
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)