+ -
当前位置:首页 → 问答吧 → 共享一个smtp带附件功能的邮件发送类

共享一个smtp带附件功能的邮件发送类

时间:2011-01-02

来源:互联网

  1. <?php
  2. /******************************************
  3. * CSMTP class v 1.0 *
  4. * by CayLeung *
  5. * copyright (c) 2008 CayLeung *
  6. *******************************************
  7. * [email protected] *
  8. *******************************************
  9. * this script is free to use only if this *
  10. * copyright statement is not removed *
  11. *******************************************/
  12. class CSMTP{
  13. protected $_emailaddr;
  14. protected $_mailhost;
  15. protected $_smtpport = 25;
  16. protected $_smtptimeout = 30;
  17. protected $_smtpgetanswertimeout = 10;
  18. protected $_smtpheaders = array();
  19. protected $_smtpbody = "";
  20. protected $_smtpboundary = "";
  21. protected $_smtpattachfile;
  22. protected $_debug = 1;

  23. function __construct($email,$password){ //发邮件的邮箱地址和密码
  24. $this ->_emailaddr = $email;
  25. $temp = explode("@",$email);
  26. $this ->_user = trim($temp[0]);
  27. $this ->_host = trim($temp[1]);
  28. $this ->_password = $password;
  29. $this ->_smtphost = "smtp.".$this ->_host;
  30. }

  31. public function linkSMTP(){ //连接smtp服务器
  32. $this ->_smtpfs = @fsockopen($this ->_smtphost,$this ->_smtpport, $errorn, $this ->_errorstr, $this ->_smtptimeout);
  33. if(!$this ->checkOK($this ->getAnswer($this ->_smtpfs),"220"))$this -> outbug("Can't link ".$this ->_smtphost);
  34. $this ->sendCommand($this ->_smtpfs, "HELO ".$this ->_host."\r\n","250");
  35. $this ->sendCommand($this ->_smtpfs, "EHLO ".$this ->_host."\r\n","250");
  36. $this ->sendCommand($this ->_smtpfs, "AUTH LOGIN\r\n","334");
  37. $this ->sendCommand($this ->_smtpfs, $this ->encode($this ->_user)."\r\n","334");
  38. $this ->sendCommand($this ->_smtpfs, $this ->encode($this ->_password)."\r\n","235");
  39. }

  40. public function quitSMTP(){ //离开服务器
  41. $this ->sendCommand($this ->_smtpfs, "QUIT\r\n","221");
  42. fclose($this ->_smtpfs);
  43. }

  44. public function attachFile(){ //加入附件,支持数组或单文件
  45. foreach(func_get_args() as $filename){
  46. if(!file_exists($filename))
  47. if(!file_exists(dirname($_SERVER['SCRIPT_FILENAME'])."/".$filename))break;
  48. $handle = fopen($filename,"rb");
  49. $str = fread($handle ,filesize($filename));
  50. fclose($handle);
  51. $temp_mime = include("mime.php");
  52. $temp_arr = explode("." ,basename($filename));
  53. $temp_key = array_slice($temp_arr, -1, 1);
  54. $filemime = isset($temp_mime[$temp_key[0]])?$temp_mime[$temp_key[0]]:"application/octet-stream";
  55. $temp = "Content-Type: ".$filemime.";\r\n";
  56. $temp .= " name=\"".$this ->encode(basename($filename),1)."\"\r\n";
  57. $temp .= "Content-Transfer-Encoding: base64\r\n";
  58. $temp .= "Content-Disposition: attachment;\r\n";
  59. $temp .= " filename=\"".$this ->encode(basename($filename),1)."\"\r\n";
  60. $temp .= "\r\n";
  61. $temp .= $this ->cutFile($this ->encode($str));
  62. $this ->_smtpattachfile[] = $temp;
  63. }
  64. }

  65. public function sendMail(){ //发送邮件
  66. foreach($this ->_smtpmialto as $mailadd_val){
  67. $filestate = count($this ->_smtpattachfile)>0 ? 1 : 0;
  68. $this ->sendCommand($this ->_smtpfs, "MAIL FROM:<".$this ->_emailaddr.">\r\n","250");
  69. $this ->sendCommand($this ->_smtpfs, "RCPT TO:<".$mailadd_val.">\r\n","250");
  70. foreach($this ->_smtpheaders as $val)
  71. $temp .= $val;
  72. $temp .= "--".$this ->_smtpboundary."\r\n";
  73. if($filestate==1){
  74. $new_smtpboundary = "--=".md5(microtime());
  75. $temp .= "Content-Type: multipart/alternative;\r\n";
  76. $temp .= " boundary=\"".$new_smtpboundary."\"\r\n";
  77. $temp .= "\r\n";
  78. $temp .= "--".$new_smtpboundary."\r\n";
  79. }
  80. $temp .= "Content-Type: ".$this ->_smtpmialmime."\r\n";
  81. $temp .= "Content-Transfer-Encoding: base64\r\n";
  82. $temp .= "\r\n";
  83. $temp .= $this ->encode($this ->_smtpcontent)."\r\n";
  84. $temp .= "\r\n";
  85. if($filestate==1){
  86. $temp .= "--".$new_smtpboundary."--\r\n";
  87. $temp .= "\r\n";
  88. foreach($this ->_smtpattachfile as $val){
  89. $temp .= "--".$this ->_smtpboundary."\r\n";
  90. $temp .= $val;
  91. }
  92. }
  93. $temp .= "--".$this ->_smtpboundary."--\r\n";
  94. echo $temp .= ".\r\n";
  95. $this ->sendCommand($this ->_smtpfs, "DATA\r\n","354");
  96. $this ->sendCommand($this ->_smtpfs, $temp,"250");
  97. unset($temp);
  98. }
  99. }

  100. public function buildMail($to ,$subject,$content ,$mime ,$username=""){ //建立邮件,建立邮件文件头
  101. if(is_array($to)){
  102. $this ->_smtpmialto = $to;
  103. }else{
  104. $this ->_smtpmialto[] = $to;
  105. }
  106. $this ->_smtpmialfrom = $this ->_emailaddr;
  107. $this ->_smtpboundary = "--=".md5(microtime());
  108. $this ->_smtpmialmime = $mime;
  109. $headers["date"] = "Date: ".date("r")."\r\n";
  110. $headers["subject"] = "Subject: ".$this ->encode($subject ,1)."\r\n";
  111. $headers["message_id"] = "Message-Id: <".md5(uniqid(microtime())) ."@".$this ->_host.">\r\n";
  112. $username = $username==""?$this ->_user:$username;
  113. $headers["from"] = "From: ".$this ->encode($username ,1)." <".$this ->_emailaddr.">\r\n";
  114. $temp = explode("@",$this ->_smtpmialto);
  115. $user = trim($temp[0]);
  116. $headers["to"] = "To: ".$this ->encode($user,1)." <".$this ->_smtpmialto.">\r\n";
  117. $headers["content_type"] = "Content-Type: multipart/mixed ";
  118. $headers["boundary"] = "; boundary=\"".$this ->_smtpboundary ."\"; charset=\"utf-8\"\r\n";
  119. $headers["content_transfer_encoding"] = "Content-Transfer-Encoding: base64\r\n";
  120. $headers["other"] = "X-Power-by: CayLeung\r\n";
  121. $headers["version"] = 'MIME-Version: 1.0' . "\r\n";
  122. $headers["end"] = "\r\n";
  123. $this ->_smtpheaders = $headers;
  124. $this ->_smtpcontent = $content;
  125. }

  126. protected function getAnswer($socket){ //获取服务器返回信息
  127. $starttime = time();
  128. $timeout = $this ->_smtpgetanswertimeout ;
  129. $this ->checkLink($this ->_smtpfs);
  130. $results = $line = "";
  131. while(true){
  132. $line = fgets($this ->_smtpfs,4096);
  133. $results .= $line;
  134. if(strpos($line,"\r\n")!==false){
  135. break;
  136. }
  137. if((time() - $starttime)>$timeout){
  138. $this -> _state = 0;
  139. break;
  140. }
  141. }
  142. $this -> _wait = 0;
  143. return $results;
  144. }

  145. protected function sendCommand($socket,$command,$okstr){ //发送命令
  146. echo "C: ".$command."<br/>";
  147. $this ->checkLink($this ->_smtpfs);
  148. fwrite($socket,$command);
  149. $r = $this ->getAnswer($socket)."<br/>";
  150. echo "S: ".$r;
  151. return $r;
  152. //echo $r= $this ->checkOK($r,$okstr)?"OK":"KO";
  153. }


  154. protected function isMailAddr($addr){ //判断是否为邮箱地址
  155. $r = preg_match("'\w[\w]+@[\w]+\.(com|cn|org)'iU",$addr)==0?false:true;
  156. return $r;
  157. }

  158. protected function encode($str,$mode=0){ //进行base64编码
  159. switch($mode){
  160. case 0:
  161. return base64_encode($str);
  162. case 1:
  163. return "=?UTF8?B?".base64_encode($str)."?=";
  164. default:
  165. return $str;
  166. }
  167. }

  168. protected function cutFile($str ,$len = 80){ //文件切割传输
  169. $strlen = strlen($str);
  170. $i = 0;
  171. $temp = "";
  172. while($strlen>0){
  173. $temp .= substr($str,$i*$len,$len);
  174. $temp .= "\r\n";
  175. $strlen -= $len;
  176. $i++;
  177. }
  178. return $temp;
  179. }

  180. protected function checkOK($str,$okstr){ //判断服务器返回值是否成功
  181. return strtoupper(substr($str,0,strlen($okstr)))==$okstr;
  182. }

  183. protected function checkLink($socket){ //检查连接是否断开
  184. if (!$socket) $this ->outbug($this ->_errorstr);
  185. }

  186. protected function outbug($str){ //显示错误信息
  187. if(!$this ->_debug)return false;
  188. echo "Catch a bug : ".$str;
  189. exit;
  190. }
  191. }

  192. /*
  193. 发送邮箱和密码
  194. */
  195. /*
  196. $e = new CSMTP("[email protected]","1119824913");

  197. //连接服务器
  198. $e -> linkSMTP();

  199. #建立邮件,
  200. #第一参数为邮箱地址Array or String
  201. #第二参数为邮件标题String
  202. #第三参数为邮件内容String
  203. #第四参数为邮件格式String

  204. $e -> buildMail(array("[email protected]","[email protected]") ,"Hi Cay" ,"Just测试 say hi!!哇哈哈哈","text/html");

  205. #添加附件
  206. #绝对路径或相对路径,支持多文件

  207. $e -> attachFile("wenti.php");

  208. #发送文件
  209. $e -> sendMail();

  210. #离开服务器
  211. $e -> quitSMTP();
  212. */
  213. ?>
复制代码

作者: tangxuewu   发布时间: 2011-01-02

犀利 啊

作者: liuxiaoqing437   发布时间: 2011-01-02

犀利 啊

作者: liuxiaoqing437   发布时间: 2011-01-02

相关阅读 更多