日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > php教程 > 利用php中mail函數發送帶有附件的郵件

利用php中mail函數發送帶有附件的郵件

來源:程序員人生   發布時間:2014-06-02 22:00:39 閱讀次數:4433次

mail函數,發送郵件

語法: mail(to,subject,message,headers,parameters)

to 規定郵件的接收者 

subject 規定郵件的主題。該參數不能包含任何換行字符 

message 規定要發送的消息 

headers 規定額外的報頭,比如 From, Cc 以及 Bcc 

parameters 規定 sendmail 程序的額外參數。 

碰到的主要問題是亂碼問題,剛開始是某些客戶端接收郵件時好(比如QQ郵箱,估計帶自動那個識別編碼)的有些不foxmail、ipad顯示亂碼,解決方式正確的設置這個mail的headers就行了,下面是我使用的完美的無亂碼的例子。

在PHP中配置php.ini文件過程分為兩個步驟:

1.先找到你放置所有PHP,Apache,MySQL文件的地方,在PHP文件夾里你可以發現有一個文件:php.ini,打開后,找到mail function地方,將原來的配置代碼改為如下(僅對windows系統):

  1. [mail function] 
  2. ; For Win32 only. 
  3. SMTP =smtp.sohu.com     
  4. mtp_port=25 
  5. ; For Win32 only. 
  6. sendmail_from = 填上你的電子郵件全稱。 

此處為以sohu的郵件服務器設置,如果你用163的郵箱,則設置為:smtp.163.com

2.在C盤搜索php.ini,選擇不是快捷方式的那一個php.ini,應該在C/WINDOWS里面的,打開它,如上面一樣修改它,保存,設置完后,記得重啟Apache服務器,然后mail()函數就可以用了,示例代碼如下:

  1. <?php 
  2. // 當發送 HTML 電子郵件時,請始終設置 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. ?> 

上面函數不可以帶附件了,下面我們升級一下,代碼如下:

  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();//生成結尾換行符 
  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. * 根據系統類型設置換行符 
  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需要是絕對地址 
  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(); 

示例代碼,有些變量需要上下文環境,代碼如下:

  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(); 

優點:使用方便就一個簡單的函數

缺點:需要php.ini支持該函數,如果某些服務器不支持而又不能改環境那就不行了而且總是不穩定,發的有時能收到有時不能.

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 福利视频一区 | 成人高清av | 91久久久久久久 | 粉嫩久久99精品久久久久久夜 | 在线国产精品视频 | 亚洲欧洲无码一区二区三区 | 亚洲福利精品 | 国产三区视频 | 亚洲h视频 | 在线成人www免费观看视频 | 精品久久久久久久久久久下田 | 久久久久中文字幕 | 亚洲精品视频免费 | 亚洲欧美在线播放 | 日韩国产一区二区 | 久久嫩草精品久久久精品 | 日本一区二区在线播放 | 久久久久国产精品一区三寸 | 不卡在线一区 | 欧美一区二区三区婷婷月色 | 日韩欧美在线不卡 | 久久成人久久爱 | 成人性生交大片免费看在线播放 | 91精品入口 | 久久久久国产精品免费免费搜索 | 91av视频网 | 97在线免费观看视频 | 成人性生交大片 | 午夜视频一区二区三区 | 国产成人精品在线 | 亚洲精品自拍 | 国产一级视频 | 亚洲一级在线 | 91精品久久久久久久 | 精品伦理一区二区 | 国产亚洲精品久久久久久 | 国产成人精品一区二区三区视频 | av中文字幕一区二区 | 一区二区电影 | 日韩小视频 | 成人在线国产 |