1. 程式人生 > >使用php發郵件二(發郵件流程)

使用php發郵件二(發郵件流程)

傳送郵件過程:
1、配置好你的郵箱服務(qq郵箱為例)
2、使用socket連線,建立一個套接字$fp=fsockopen($hostname,$port,$errno,$errmsg,30)
3、向對方郵件伺服器發出的標識自己的身份的命令fputs($fp,"EHLO ki\r\n");
4、即將進行身份認證fputs($fp,"AUTH LOGIN\r\n")
5、傳送使用者名稱fputs($fp, base64_encode($sendmail_from)."\r\n");
6、傳送密碼fputs($fp, base64_encode($sendmail_psw)."\r\n");


7、告訴對方本次郵件的傳送人是誰
fputs($fp,"MAIL FROM: <".preg_replace("/.*\&amp;amp;lt;(.+?)\>.*/","\\1",$mail_from).">\r\n");
8、傳送給誰fputs($fp, "RCPT TO: <".preg_replace("/.*\<(.+?)\>.*/", "\\1", $touser).">\r\n");
9、告訴對方本次郵件,接下來我們傳送郵件具體內容了fputs($fp, "DATA\r\n");
10、傳送內容包括頭部和body
11、郵件內容輸入完畢,執行指令退出fputs($fp, "QUIT\r\n");

程式碼:

<?php function dmail($mail_to,$mail_subject,$mail_body,$mail_from='',$mail_sign=true){ $sendmail_from = '@qq.com';//你的郵箱 $sendmail_psw = '';//授權碼 $hostname = 'smtp.qq.com'; $port=25; $mail_from = "=?utf-8?B?".base64_encode('tt').'?= <'.$sendmail_from.'>';//這裡的=?utf-8?B?標識要寫上,如果傳送的內容含有中文字元,帶上這個標識不會出現亂碼 $mail_subject
= "=?utf-8?B?".base64_encode($mail_subject)."?="; //主題 $mail_body = stripslashes($mail_body); //內容 $mail_body = chunk_split(base64_encode(str_replace("\r\n.", " \r\n..", str_replace("\n", "\r\n", str_replace("\r", "\n", str_replace("\r\n", "\n", str_replace("\n\r", "\r", $mail_body)))))));//將\n\r替換為\r //chunk_split把字串分割為一連串更小的部分 $mail_dlmt="\r\n";//分隔符 $headers = ''; $headers .= "From: $mail_from".$mail_dlmt; $headers .= "X-Priority: 3".$mail_dlmt; $headers .= "X-Mailer: ki".$mail_dlmt; $headers .= "MIME-Version: 1.0".$mail_dlmt; //MIME的版本 $headers .= "Content-type: text/html; charset=utf-8".$mail_dlmt; //郵件內容的格式 $headers .= "Content-Transfer-Encoding: base64".$mail_dlmt; $host = $hostname.':'.$port.' ' ; if(!$fp=fsockopen($hostname,$port,$errno,$errmsg,30)){ //建立一個套接字 echo "$host can not connect to the SMTP server"; return; } stream_set_blocking($fp,true); $RE =fgets($fp,512); if(substr($RE,0,3) !='220'){ echo 'ERROR'.$host.$RE; return 0; } //向對方郵件伺服器發出的標識自己的身份的命令 fputs($fp,"EHLO ki\r\n"); $RE = fgets($fp,512); if(substr($RE,0,3)!=220&&substr($RE,0,3) !=250){ $errmsg = $host.'HELO/EHLO - '.$RE; echo $errmsg; return 0 ; } while(1){ if(substr($RE,3,1) != '-' || empty($RE)) break; $RE = fgets($fp,512); } if(1){ fputs($fp,"AUTH LOGIN\r\n");//即將進行身份認證 $RE = fgets($fp,512); if(substr($RE,0,3)!=334){ exit("ERROR:$host AUTH LOGIN - $RE"); } fputs($fp, base64_encode($sendmail_from)."\r\n"); //使用者名稱使用base64編碼才行 $RE = fgets($fp, 512);//334 if(substr($RE, 0, 3) != 334) { $errmsg = $host.'USERNAME - '.$RE; exit($errmsg); } fputs($fp, base64_encode($sendmail_psw)."\r\n"); //密碼使用base64編碼才行 $RE = fgets($fp, 512);//235 Authentication successful if(substr($RE, 0, 3) != 235) { $errmsg = $host.'PASSWORD - '.$RE; exit($errmsg); } $mail_from = $sendmail_from; } fputs($fp,"MAIL FROM: <".preg_replace("/.*\<(.+?)\>.*/","\\1",$mail_from).">\r\n"); //告訴對方本次郵件傳送人是誰 $RE = fgets($fp,512); if(substr($RE, 0, 3) != 250) { //出錯再發一次 fputs($fp, "MAIL FROM: <".preg_replace("/.*\<(.+?)\>.*/", "\\1", $mail_from).">\r\n"); $RE = fgets($fp, 512); if(substr($RE, 0, 3) != 250) { $errmsg = $host.'MAIL FROM - '.$RE; exit($errmsg); } } foreach(explode(',',$mail_to) as $touser){ $touser = trim($touser); if($touser){ fputs($fp, "RCPT TO: <".preg_replace("/.*\<(.+?)\>.*/", "\\1", $touser).">\r\n"); //傳送給誰 $RE = fgets($fp, 512);//250 Ok if(substr($RE, 0, 3) != 250) { fputs($fp, "RCPT TO: <".preg_replace("/.*\<(.+?)\>.*/", "\\1", $touser).">\r\n"); $RE = fgets($fp, 512); $errmsg = $host.'RCPT TO - '.$RE; exit($errmsg); } } } fputs($fp, "DATA\r\n"); //告訴對方本次郵件,接下來我們傳送郵件具體內容了 $RE = fgets($fp, 512); if(substr($RE, 0, 3) != 354) { $errmsg = $host.'DATA - '.$RE; exit($errmsg); } //內容 list($msec, $sec) = explode(' ', microtime()); $headers .= "Message-ID: <".date('YmdHis', $sec).".".($msec*1000000).".".substr($mail_from, strpos($mail_from,'@')).">".$mail_dlmt; fputs($fp, "Date: ".date('r')."\r\n"); fputs($fp, "To: ".$mail_to."\r\n"); fputs($fp, "Subject: ".$mail_subject."\r\n"); fputs($fp, $headers."\r\n"); fputs($fp, "\r\n\r\n"); fputs($fp, "$mail_body\r\n.\r\n"); $RE = fgets($fp, 512);//250 Ok: queued as if(substr($RE, 0, 3) != 250) { $errmsg = $host.'END - '.$RE; exit($errmsg); } fputs($fp, "QUIT\r\n"); //221 Bye郵件內容輸入完畢後,執行該指令退出 return 'SUCCESS'; } dmail("接受者的郵箱@qq.com","主題","內容");

參考連結
https://www.jb51.net/article/112291.htm
https://blog.csdn.net/zb3288/article/details/1929438
https://blog.csdn.net/wsxqaz/article/details/6307906