多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內(nèi)最全I(xiàn)T社區(qū)平臺(tái) 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁 > php開源 > php教程 > 利用php中mail函數(shù)發(fā)送帶有附件的郵件

利用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)):

  1. [mail function] 
  2. ; For Win32 only. 
  3. SMTP =smtp.sohu.com     
  4. mtp_port=25 
  5. ; For Win32 only. 
  6. 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ù)就可以用了,示例代碼如下:

  1. <?php 
  2. // 當(dāng)發(fā)送 HTML 電子郵件時(shí),請(qǐng)始終設(shè)置 content-type 
  3. $headers = "MIME-Version: 1.0" . "rn"
  4. $headers .= "Content-type:text/html; charset=utf-8"
  5. mail($to,$subject,$message,$headers); 
  6. ?> 

上面函數(shù)不可以帶附件了,下面我們升級(jí)一下,代碼如下:

  1. <?php 
  2. class Mail { 
  3. private $topic
  4. private $toaddr
  5. private $fromaddr
  6. private $cc
  7. private $content
  8. private $attach
  9. private $header
  10. private $domain;//郵箱域 
  11. private $msg
  12. private $filename
  13. private $filemime
  14. private $filestr
  15. private $boundary
  16. private $uniqid
  17. private $eol//每行末尾所加的換行符類型 
  18.  
  19. function __construct(){ 
  20. $this->getEOT();//生成結(jié)尾換行符 
  21. $this->getUniq_id(); 
  22. $this->header=''
  23. $this->attach=''
  24. $this->cc=''
  25. $this->msg=''
  26.  
  27.  
  28. public function getFromaddr() { 
  29. return $this->fromaddr; 
  30. public function setFromaddr($fromaddr) { 
  31. $this->fromaddr = $fromaddr
  32. public function getTopic() { 
  33. return $this->topic; 
  34.  
  35. public function getToaddr() { 
  36. return $this->toaddr; 
  37.  
  38. public function getCc() { 
  39. return $this->cc; 
  40. public function getContent() { 
  41. return $this->content; 
  42.  
  43. public function getAttach() { 
  44. return $this->attach; 
  45.  
  46. public function setTopic($topic) { 
  47. $this->topic = mb_convert_encoding(trim($topic),'UTF-8','auto'); 
  48. public function setToaddr($toaddr) { 
  49. $this->toaddr = trim($toaddr); 
  50.  
  51. public function setCc($cc) { 
  52. $this->cc = trim($cc); 
  53.  
  54. public function setContent($content) { 
  55. $this->content = mb_convert_encoding(trim($content),'UTF-8','auto'); 
  56.  
  57. public function setAttach($attach) { 
  58. $this->attach = trim($attach); 
  59. public function getDomain() { 
  60. return $this->domain; 
  61. public function setDomain($domain) { 
  62. $this->domain = $domain;//輸入的值為'@domain.com' 
  63.  
  64. /* 
  65. * 根據(jù)系統(tǒng)類型設(shè)置換行符 
  66. */ 
  67. private function getEOT() { 
  68. if (strtoupper ( substr ( PHP_OS, 0, 3 ) == 'WIN' )) { 
  69. $this->eol = "rn"
  70. elseif (strtoupper ( substr ( PHP_OS, 0, 3 ) == 'MAC' )) { 
  71. $this->eol= "r"
  72. else { 
  73. $this->eol = "n"
  74.  
  75. private function getBoundary(){ 
  76. $this->boundary= '--'.substr(md5(time().rand(1000,2000)),0,16); 
  77. private function getUniq_id(){ 
  78. $this->uniqid= md5(microtime().time().rand(1,100)); 
  79. private function outputCommonHeader(){ 
  80. $this->header .= 'From: '.$this->fromaddr.$this->eol; 
  81. //$this->header .= 'To: '.$this->toaddr.$this->eol; 
  82. //$this->header .= 'Subject: '.$this->topic.$this->eol; 
  83. $this->header .= 'Message-ID: <'.$this->uniqid.$this->domain.'>'.$this->eol; 
  84. $this->header .= 'MIME-Version: 1.0'.$this->eol; 
  85. $this->header .= 'Reply-To: '.$this->fromaddr.$this->eol; 
  86. $this->header .= 'Return-Path: '.$this->fromaddr.$this->eol; 
  87. $this->header .= 'X-Mailer: Xmail System'.$this->eol; 
  88. $this->header .= 'Content-Disposition: inline'.$this->eol; 
  89. private function mime_content_type ( $f )  
  90. {  
  91. $temp = trim ( exec ('file -bi ' . escapeshellarg ( $f ) ) ) ; 
  92. $temp = preg_replace('/s+/',' ',$temp); 
  93. $temp = explode(' ',$temp); 
  94. return $temp[0]; 
  95. }//判斷文件的mime類型 
  96.  
  97. /* 
  98. * 只帶有抄送 
  99. */ 
  100. private function mailWithCC(){ 
  101. $this->header .= 'Cc: '.$this->cc.$this->eol; 
  102. $this->header .= 'Content-type: text/html; charset=UTF-8'.$this->eol; 
  103. $this->header .= 'Content-Transfer-Encoding: 8bit'.$this->eol; 
  104. $this->msg = $this->content; 
  105. if(mail($this->toaddr,$this->topic,$this->msg,$this->header)){ 
  106. return 1; 
  107. }else
  108. return 0; 
  109. /* 
  110. * $filedir需要是絕對(duì)地址 
  111. */ 
  112. private function attachmentToBase64($filedir){ 
  113. $this->filename = basename($filedir); 
  114. @$fopen = fopen($filedir,'r'); 
  115. $str = fread($fopen,filesize($filedir)); 
  116. $str = base64_encode($str); 
  117. $this->filestr = $str
  118.  
  119. /* 
  120. * 只帶有附件 
  121. */ 
  122. private function mailWithAttach(){ 
  123. $this->attachmentToBase64($this->attach); 
  124. $this->header .= 'Content-type: multipart/mixed; boundary="'.str_replace('--','',$this->boundary).'"'.$this->eol; 
  125. $this->msg .= $this->eol.$this->boundary.$this->eol; 
  126. $this->msg .= 'Content-Type: text/html; charset=utf-8'.$this->eol; 
  127. $this->msg .= 'Content-Disposition: inline'.$this->eol; 
  128. $this->msg .= $this->eol.$this->content.$this->eol; 
  129. $this->msg .= $this->boundary.$this->eol; 
  130. $this->msg .= 'Content-Type: '.$this->mime_content_type($this->attach).$this->eol; 
  131. $this->msg .= 'Content-Disposition: attachment; filename="'.$this->filename.'"'.$this->eol; 
  132. $this->msg .= 'Content-Transfer-Encoding: base64'.$this->eol; 
  133. $this->msg .= $this->eol.$this->filestr.$this->eol; 
  134. $this->msg .= $this->eol.$this->boundary.'--'
  135. if(mail($this->toaddr,$this->topic,$this->msg,$this->header)){ 
  136. return 1; 
  137. }else
  138. return 0; 
  139. /* 
  140. * 帶有附件和抄送 
  141. */ 
  142. private function mailAll(){ 
  143. $this->attachmentToBase64($this->attach); 
  144. $this->header .= 'Cc: '.$this->cc.$this->eol; 
  145. $this->header .= 'Content-type: multipart/mixed; boundary="'.str_replace('--','',$this->boundary).'"'.$this->eol; 
  146. $this->msg .= $this->eol.$this->boundary.$this->eol; 
  147. $this->msg .= 'Content-Type: text/html; charset=utf-8'.$this->eol; 
  148. $this->msg .= 'Content-Disposition: inline'.$this->eol; 
  149. $this->msg .= $this->eol.$this->content.$this->eol; 
  150. $this->msg .= $this->boundary.$this->eol; 
  151. $this->msg .= 'Content-Type: '.$this->mime_content_type($this->attach).$this->eol; 
  152. $this->msg .= 'Content-Disposition: attachment; filename="'.$this->filename.'"'.$this->eol; 
  153. $this->msg .= 'Content-Transfer-Encoding: base64'.$this->eol; 
  154. $this->msg .= $this->eol.$this->filestr.$this->eol; 
  155. $this->msg .= $this->eol.$this->boundary.'--'
  156. if(mail($this->toaddr,$this->topic,$this->msg,$this->header)){ 
  157. return 1; 
  158. }else
  159. return 0; 
  160. /* 
  161. * 不帶抄送和附件 
  162. */ 
  163. private function mailSimple(){ 
  164. $this->header .= 'Content-type: text/html; charset=UTF-8'.$this->eol; 
  165. $this->header .= 'Content-Transfer-Encoding: 8bit'.$this->eol; 
  166. $this->msg = $this->content; 
  167. if(mail($this->toaddr,$this->topic,$this->msg,$this->header)){ 
  168. return 1; 
  169. }else
  170. return 0; 
  171. public function send(){ 
  172. if(emptyempty($this->attach)&&emptyempty($this->cc)){ 
  173. $this->outputCommonHeader(); 
  174. return $this->mailSimple(); 
  175. }else if(emptyempty($this->attach)){ 
  176. $this->outputCommonHeader(); 
  177. return $this->mailWithCC(); 
  178. }else if(emptyempty($this->cc)){ 
  179. $this->outputCommonHeader(); 
  180. $this->getBoundary(); //有附件就生成boundary 
  181. return $this->mailWithAttach(); 
  182. }else if(!emptyempty($this->toaddr)&&!emptyempty($this->topic)&&!emptyempty($this->cc)&&!emptyempty($this->content)&&!emptyempty($this->attach)){ 
  183. $this->outputCommonHeader(); 
  184. $this->getBoundary(); //有附件就生成boundary 
  185. return $this->mailAll(); 

示例代碼,有些變量需要上下文環(huán)境,代碼如下:

  1. $m = new Mail(); 
  2. $m->setToaddr($this->temp['receipt_address']); 
  3. $m->setTopic($this->temp['mail_title']); 
  4. $m->setContent($this->temp['mail_content']); 
  5. $m->setFromaddr($_SESSION['user']['name'].' <'.$_SESSION['user']['name'].'@'.SystemDomain.'>'); 
  6. $m->setDomain('@'.SystemDomain); 
  7. $m->setCc($this->temp['cc_address']); 
  8. $m->setAttach(PATH.'/temp/'.$this->temp['attachment_file']); 
  9. $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)
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 中文字幕丰满乱码 | 国产美女视频一区二区二三区 | 欧美军人男同69gay | 日本xxxxxbbbbb精品| 毛片最新网址 | 免费观看亚洲 | 中国精品久久 | 成年人小视频在线观看 | 免费国产高清精品一区在线 | 老司机免费福利视频 | 午夜一级做a爰片久久毛片 午夜一区二区三区 | 国外处破女一区二区 | 成 人国产在线观看高清不卡 | 羞羞网页登界面入口 | 亚洲国产欧洲精品路线久久 | 国产免费久久精品久久久 | 亚洲狠狠| 日韩精品一区二区三区中文字幕 | 宅男午夜大片啪啪软件 | 亚洲欧美另类小说 | 亚洲精品无码不卡 | 亚洲无av码一区二区三区 | 一区二区三区在线 | 日本 | 美女h在线观看 | 成人精品美女隐私漫画 | 国产欧美一区二区另类精品 | 最近中文字幕国语免费完整 | 美国毛片亚洲社区在线观看 | 午夜影院福利 | 国产精品久久久久久一区二区 | 91久久在线| 韩国理论片在线观看bd | 伊人久久五月 | 性欧美v | 名优写真一区二区在线 | 欧美黑人巨大最猛性xxxxx | 美女教师一级毛片 | 国产亚洲精品九九久在线观看 | 青青青青久久精品国产一百度 | 亚洲视频1 | 日韩高清无砖砖区2022 |