php发送邮件代码

  1. <?php
  2. class smtp
  3. {
  4. /* Public Variables */
  5. var $smtp_port;
  6. var $time_out;
  7. var $host_name;
  8. var $log_file;
  9. var $relay_host;
  10. var $debug;
  11. var $auth;
  12. var $user;
  13. var $pass;
  14. /* Private Variables */
  15. var $sock;
  16. /* Constractor */
  17. function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass)
  18. {
  19. $this->debug = FALSE;
  20. $this->smtp_port = $smtp_port;
  21. $this->relay_host = $relay_host;
  22. $this->time_out = 30; //is used in fsockopen()
  23. #
  24. $this->auth = $auth;//auth
  25. $this->user = $user;
  26. $this->pass = $pass;
  27. #
  28. $this->host_name = "localhost"; //is used in HELO command
  29. $this->log_file ="";
  30. $this->sock = FALSE;
  31. }
  32. /* Main Function */
  33. function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")
  34. {
  35. $mail_from = $this->get_address($this->strip_comment($from));
  36. $body = ereg_replace("(^|(rn))(\.)", "[url=file://\1.\3]\1.\3[/url]", $body);
  37. $header .= "MIME-Version:1.0rn";
  38. if($mailtype=="HTML"){
  39. $header .= "Content-Type:text/htmlrn ";
  40. }
  41. $header .= "To: ".$to."rn";
  42. if ($cc != "") {
  43. $header .= "Cc: ".$cc."rn";
  44. }
  45. $header .= "From: $from<".$from.">rn";
  46. $header .= "Subject: ".$subject."rn";
  47. $header .= $additional_headers;
  48. $header .= "Date: ".date("r")."rn";
  49. $header .= "X-Mailer:By Redhat (PHP/".phpversion().")rn";
  50. list($msec, $sec) = explode(" ", microtime());
  51. $header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">rn";
  52. $TO = explode(",", $this->strip_comment($to));
  53. if ($cc != "") {
  54. $TO = array_merge($TO, explode(",", $this->strip_comment($cc)));
  55. }
  56. if ($bcc != "") {
  57. $TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));
  58. }
  59. $sent = TRUE;
  60. foreach ($TO as $rcpt_to) {
  61. $rcpt_to = $this->get_address($rcpt_to);
  62. if (!$this->smtp_sockopen($rcpt_to)) {
  63. $this->log_write("Error: Cannot send email to ".$rcpt_to."n");
  64. $sent = FALSE;
  65. continue;
  66. }
  67. if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {
  68. $this->log_write("E-mail has been sent to <".$rcpt_to.">n");
  69. } else {
  70. $this->log_write("Error: Cannot send email to <".$rcpt_to.">n");
  71. $sent = FALSE;
  72. }
  73. fclose($this->sock);
  74. $this->log_write("Disconnected from remote hostn");
  75. }
  76. echo "<br>";
  77. echo $header;
  78. return $sent;
  79. }
  80. /* Private Functions */
  81. function smtp_send($helo, $from, $to, $header, $body = "")
  82. {
  83. if (!$this->smtp_putcmd("HELO", $helo)) {
  84. return $this->smtp_error("sending HELO command");
  85. }
  86. #auth
  87. if($this->auth){
  88. if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {
  89. return $this->smtp_error("sending HELO command");
  90. }
  91. if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
  92. return $this->smtp_error("sending HELO command");
  93. }
  94. }
  95. #
  96. if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">")) {
  97. return $this->smtp_error("sending MAIL FROM command");
  98. }
  99. if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">")) {
  100. return $this->smtp_error("sending RCPT TO command");
  101. }
  102. if (!$this->smtp_putcmd("DATA")) {
  103. return $this->smtp_error("sending DATA command");
  104. }
  105. if (!$this->smtp_message($header, $body)) {
  106. return $this->smtp_error("sending message");
  107. }
  108. if (!$this->smtp_eom()) {
  109. return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]");
  110. }
  111. if (!$this->smtp_putcmd("QUIT")) {
  112. return $this->smtp_error("sending QUIT command");
  113. }
  114. return TRUE;
  115. }
  116. function smtp_sockopen($address)
  117. {
  118. if ($this->relay_host == "") {
  119. return $this->smtp_sockopen_mx($address);
  120. } else {
  121. return $this->smtp_sockopen_relay();
  122. }
  123. }
  124. function smtp_sockopen_relay()
  125. {
  126. $this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."n");
  127. $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
  128. if (!($this->sock && $this->smtp_ok())) {
  129. $this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."n");
  130. $this->log_write("Error: ".$errstr." (".$errno.")n");
  131. return FALSE;
  132. }
  133. $this->log_write("Connected to relay host ".$this->relay_host."n");
  134. return TRUE;;
  135. }
  136. function smtp_sockopen_mx($address)
  137. {
  138. $domain = ereg_replace("[url=mailto:^.+@([^@]+)$]^.+@([^@]+)$[/url]", "[url=file://\1]\1[/url]", $address);
  139. if (!@getmxrr($domain, $MXHOSTS)) {
  140. $this->log_write("Error: Cannot resolve MX "".$domain.""n");
  141. return FALSE;
  142. }
  143. foreach ($MXHOSTS as $host) {
  144. $this->log_write("Trying to ".$host.":".$this->smtp_port."n");
  145. $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
  146. if (!($this->sock && $this->smtp_ok())) {
  147. $this->log_write("Warning: Cannot connect to mx host ".$host."n");
  148. $this->log_write("Error: ".$errstr." (".$errno.")n");
  149. continue;
  150. }
  151. $this->log_write("Connected to mx host ".$host."n");
  152. return TRUE;
  153. }
  154. $this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")n");
  155. return FALSE;
  156. }
  157. function smtp_message($header, $body)
  158. {
  159. fputs($this->sock, $header."rn".$body);
  160. $this->smtp_debug("> ".str_replace("rn", "n"."> ", $header."n> ".$body."n> "));
  161. return TRUE;
  162. }
  163. function smtp_eom()
  164. {
  165. fputs($this->sock, "rn.rn");
  166. $this->smtp_debug(". [EOM]n");
  167. return $this->smtp_ok();
  168. }
  169. function smtp_ok()
  170. {
  171. $response = str_replace("rn", "", fgets($this->sock, 512));
  172. $this->smtp_debug($response."n");
  173. if (!ereg("^[23]", $response)) {
  174. fputs($this->sock, "QUITrn");
  175. fgets($this->sock, 512);
  176. $this->log_write("Error: Remote host returned "".$response.""n");
  177. return FALSE;
  178. }
  179. return TRUE;
  180. }
  181. function smtp_putcmd($cmd, $arg = "")
  182. {
  183. if ($arg != "") {
  184. if($cmd=="") $cmd = $arg;
  185. else $cmd = $cmd." ".$arg;
  186. }
  187. fputs($this->sock, $cmd."rn");
  188. $this->smtp_debug("> ".$cmd."n");
  189. return $this->smtp_ok();
  190. }
  191. function smtp_error($string)
  192. {
  193. $this->log_write("Error: Error occurred while ".$string.".n");
  194. return FALSE;
  195. }
  196. function log_write($message)
  197. {
  198. $this->smtp_debug($message);
  199. if ($this->log_file == "") {
  200. return TRUE;
  201. }
  202. $message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;
  203. if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {
  204. $this->smtp_debug("Warning: Cannot open log file "".$this->log_file.""n");
  205. return FALSE;
  206. }
  207. flock($fp, LOCK_EX);
  208. fputs($fp, $message);
  209. fclose($fp);
  210. return TRUE;
  211. }
  212. function strip_comment($address)
  213. {
  214. $comment = "[url=file://\([^()]*\]\([^()]*\[/url])";
  215. while (ereg($comment, $address)) {
  216. $address = ereg_replace($comment, "", $address);
  217. }
  218. return $address;
  219. }
  220. function get_address($address)
  221. {
  222. $address = ereg_replace("([ trn])+", "", $address);
  223. $address = ereg_replace("^.*<(.+)>.*$", "[url=file://\1]\1[/url]", $address);
  224. return $address;
  225. }
  226. function smtp_debug($message)
  227. {
  228. if ($this->debug) {
  229. echo $message."<br>";
  230. }
  231. }
  232. function get_attach_type($image_tag) { //
  233. $filedata = array();
  234. $img_file_con=fopen($image_tag,"r");
  235. unset($image_data);
  236. while ($tem_buffer=AddSlashes(fread($img_file_con,filesize($image_tag))))
  237. $image_data.=$tem_buffer;
  238. fclose($img_file_con);
  239. $filedata['context'] = $image_data;
  240. $filedata['filename']= basename($image_tag);
  241. $extension=substr($image_tag,strrpos($image_tag,"."),strlen($image_tag)-strrpos($image_tag,"."));
  242. switch($extension){
  243. case ".gif":
  244. $filedata['type'] = "image/gif";
  245. break;
  246. case ".gz":
  247. $filedata['type'] = "application/x-gzip";
  248. break;
  249. case ".htm":
  250. $filedata['type'] = "text/html";
  251. break;
  252. case ".html":
  253. $filedata['type'] = "text/html";
  254. break;
  255. case ".jpg":
  256. $filedata['type'] = "image/jpeg";
  257. break;
  258. case ".tar":
  259. $filedata['type'] = "application/x-tar";
  260. break;
  261. case ".txt":
  262. $filedata['type'] = "text/plain";
  263. break;
  264. case ".zip":
  265. $filedata['type'] = "application/zip";
  266. break;
  267. default:
  268. $filedata['type'] = "application/octet-stream";
  269. break;
  270. }
  271. return $filedata;
  272. }
  273. }
  274. ?>

send.php:

$title = $_POST['title'];

$Message = $_POST['Message'];

$obj_title = iconv('UTF-8','Shift-JIS',$title);

$obj_message = iconv('UTF-8', 'Shift-JIS',$Message);

$smtpserver = "smtp.163.com";//SMTP服务器

$smtpserverport =25;//SMTP服务器端口

$smtpusermail = "";//SMTP服务器的用户邮箱

$smtpemailto = "";//发送给谁

$smtpuser = ""; //SMTP服务器的用户帐号

$smtppass = "";//SMTP服务器的用户密码

$mailsubject = $title;//邮件主题

$mailbody = $Message ;//邮件内容

$mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件

$smtp = new

smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一true是表示使用身份验证,否则不使用身份验证.

$smtp->debug = true;//是否显示发送的调试信息

$smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);