[php] php curl smtp發送郵件

來源:互聯網
上載者:User
公司的雲平台把fsockopen關掉了,如果要使用smtp外網的郵箱來發送郵件的話 只能試試使用curl來進行了

先google了一下,發現很多問相關問題的但沒有相關的解答,在phpclasses裡也沒有找到相關的類於是自己邊看stmp的相關協議邊開始嘗試curl

SMTP協議

這個在網上可以找到多相關的例子,可以自己實驗一下使用telnet去串連mail伺服器

$ telnet 郵箱SMTP服務地址 25Trying 郵箱服務IP地址...Connected to 郵箱SMTP服務地址.Escape character is '^]'.220 exchange郵箱伺服器位址 Microsoft ESMTP MAIL Service ready at Sat, 2 Jun 2012 15:02:12 +0800EHLO 127.0.0.1250-exchange郵箱伺服器位址 Hello [郵箱服務IP地址]250-SIZE250-PIPELINING250-DSN250-ENHANCEDSTATUSCODES250-X-ANONYMOUSTLS250-AUTH NTLM LOGIN250-X-EXPS GSSAPI NTLM250-8BITMIME250-BINARYMIME250-CHUNKING250-XEXCH50250 XRDSTAUTH LOGIN334 VXNlcm5hbWU6使用者名稱(base64_encode)334 UGFzc3dvcmQ6密碼(base64_encode)235 2.7.0 Authentication successfulMAIL FROM:寄件匣地址250 2.1.0 Sender OKRCPT TO:收件匣地址250 2.1.5 Recipient OKDATA354 Start mail input; end with .要發送的內容(這裡的相關的規範有很多).250 2.6.0 <0b476f30-3b96-4e3d-84d2-395a96d34000@exchange郵箱伺服器位址> Queued mail for deliveryQUIT221 2.0.0 Service closing transmission channelConnection closed by foreign host.

php測試代碼:

 1  "郵箱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 "
\r\n";61 var_dump($content);62 63 // 結束64 curl_close($curl);

以上只是測試的php

包測試+修改花了近6個小時讓產品的代碼相容了fsockopen和curl

以後有空寫個相容fsockopen和curl簡單發送郵件的smtp類

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.