[php]php curl smtp發送郵件

公司的雲平臺把fsockopen關掉瞭,如果要使用smtp外網的郵箱來發送郵件的話 隻能試試使用curl來進行瞭
先google瞭一下,發現很多問相關問題的但沒有相關的解答,在phpclasses裡也沒有找到相關的類於是自己邊看stmp的相關協議邊開始嘗試curl
SMTP協議
這個在網上可以找到多相關的例子,可以自己實驗一下使用telnet去連接mail伺服器
 
$ telnet 郵箱SMTP服務地址 25
Trying 郵箱服務IP地址…
Connected to 郵箱SMTP服務地址.
Escape character is '^]'.
220 exchange郵箱伺服器地址 Microsoft ESMTP MAIL Service ready at Sat, 2 Jun 2012 15:02:12 +0800
EHLO 127.0.0.1
250-exchange郵箱伺服器地址 Hello [郵箱服務IP地址]
250-SIZE
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-X-ANONYMOUSTLS
250-AUTH NTLM LOGIN
250-X-EXPS GSSAPI NTLM
250-8BITMIME
250-BINARYMIME
250-CHUNKING
250-XEXCH50
250 XRDST
AUTH LOGIN
334 VXNlcm5hbWU6
用戶名(base64_encode)
334 UGFzc3dvcmQ6
密碼(base64_encode)
235 2.7.0 Authentication successful
MAIL FROM:發件箱地址
250 2.1.0 Sender OK
RCPT TO:收件箱地址
250 2.1.5 Recipient OK
DATA
354 Start mail input; end with <CRLF>.<CRLF>
要發送的內容(這裡的相關的規范有很多)
.
250 2.6.0 <0b476f30-3b96-4e3d-84d2-395a96d34000@exchange郵箱伺服器地址> Queued mail for delivery
QUIT
221 2.0.0 Service closing transmission channel
Connection closed by foreign host.
 
php測試代碼:
 
 1 <?php
 2 header("content-type:text/html;charset=utf-8");
 3 $smtp = array(
 4     "url"      => "郵箱SMTP伺服器地址",
 5     "port"     => "郵箱SMTP伺服器端口", // 一般為25
 6     "username" => "用戶名",
 7     "password" => "密碼",
 8     "from"     => "發件地址",
 9     "to"       => "收件地址",
10     "subject"  => "測試一下標題",
11     "body"     => "測試一下內容"
12 );
13
14 $CRLF = "\r\n";
15 $test = "";
16 $curl = curl_init();
17
18 curl_setopt($curl, CURLOPT_URL, $smtp['url']);
19 curl_setopt($curl, CURLOPT_PORT, $smtp['port']);
20 curl_setopt($curl, CURLOPT_TIMEOUT,10);
21
22 function inlineCode($str){
23         $str = trim($str);
24         return $str?'=?UTF-8?B?'.base64_encode($str).'?= ':'';
25 }
26
27 function buildHeader($headers){
28     $ret = '';
29     foreach($headers as $k=>$v){
30         $ret.=$k.': '.$v."\n";
31     }
32     return $ret;
33 }
34
35 //
36 $header = array(
37     'Return-path'=>'<'.$smtp['from'].'>',
38     'Date'=>date('r'),
39     'From'=> '<'.$smtp['from'].'>',
40     'MIME-Version'=>'1.0',
41     'Subject'=>inlineCode($smtp['subject']),
42     'To'=>$smtp['to'],
43     'Content-Type'=>'text/html; charset=UTF-8; format=flowed',
44     'Content-Transfer-Encoding'=>'base64'
45 );
46 $data = buildHeader($header).$CRLF.chunk_split(base64_encode($smtp['body']));
47
48
49 $content  = "EHLO ".$smtp["url"].$CRLF; // 先得hello一下
50 $content .= "AUTH LOGIN".$CRLF.base64_encode($smtp["username"]).$CRLF.base64_encode($smtp["password"]).$CRLF; // 驗證登陸
51 $content .= "MAIL FROM:".$smtp["from"].$CRLF; // 發件地址
52 $content .= "RCPT TO:".$smtp["to"].$CRLF;  // 收件地址
53 $content .= "DATA".$CRLF.$data.$CRLF.".".$CRLF; // 發送內容
54 $content .= "QUIT".$CRLF; // 退出
55
56 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);    // curl接收返回數據
57 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $content);
58 $test = curl_exec($curl);
59 var_dump($test);
60 echo "<br/>\r\n";
61 var_dump($content);
62
63 // 結束
64 curl_close($curl);
 
以上隻是測試的php
包測試+修改花瞭近6個小時讓產品的代碼兼容瞭fsockopen和curl
 
以後有空寫個兼容fsockopen和curl簡單發送郵件的smtp類
think in coding

 

 

摘自 bluefrog

You May Also Like