php中通過curl smtp發送郵件

來源:互聯網
上載者:User

先google了一下,發現很多問相關問題的但沒有相關的解答,在phpclasses裡也沒有找到相關的類於是自己邊看stmp的相關協議邊開始嘗試curl
SMTP協議
這個在網上可以找到多相關的例子,可以自己實驗一下使用telnet去串連mail伺服器 複製代碼 代碼如下:$ telnet 郵箱SMTP服務地址 25
Trying 郵箱服務IP地址...
Connected to 郵箱SMTP服務地址.
Escape character is '^]'.
exchange郵箱伺服器位址 Microsoft ESMTP MAIL Service ready at Sat, 2 Jun 2012 15:02:12 +0800
EHLO 127.0.0.1
-exchange郵箱伺服器位址 Hello [郵箱服務IP地址]
-SIZE
-PIPELINING
-DSN
-ENHANCEDSTATUSCODES
-X-ANONYMOUSTLS
-AUTH NTLM LOGIN
-X-EXPS GSSAPI NTLM
-8BITMIME
-BINARYMIME
-CHUNKING
-XEXCH50
XRDST
AUTH LOGIN
VXNlcm5hbWU6
使用者名稱(base64_encode)
UGFzc3dvcmQ6
密碼(base64_encode)
2.7.0 Authentication successful
MAIL FROM:寄件匣地址
2.1.0 Sender OK
RCPT TO:收件匣地址
2.1.5 Recipient OK
DATA
Start mail input; end with <CRLF>.<CRLF>
要發送的內容(這裡的相關的規範有很多)
.
2.6.0 <0b476f30-3b96-4e3d-84d2-395a96d34000@exchange郵箱伺服器位址> Queued mail for delivery
QUIT
2.0.0 Service closing transmission channel
Connection closed by foreign host.

php測試代碼: 複製代碼 代碼如下:<?php
header("content-type:text/html;charset=utf-8");
$smtp = array(
"url" => "郵箱SMTP伺服器位址",
"port" => "郵箱SMTP伺服器連接埠", // 一般為25
"username" => "使用者名稱",
"password" => "密碼",
"from" => "發件地址",
"to" => "收件地址",
"subject" => "測試一下標題",
"body" => "測試一下內容"
);
$CRLF = "\r\n";
$test = "";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $smtp['url']);
curl_setopt($curl, CURLOPT_PORT, $smtp['port']);
curl_setopt($curl, CURLOPT_TIMEOUT,10);
function inlineCode($str){
$str = trim($str);
return $str?'=?UTF-8?B?'.base64_encode($str).'?= ':'';
}
function buildHeader($headers){
$ret = '';
foreach($headers as $k=>$v){
$ret.=$k.': '.$v."\n";
}
return $ret;
}
//
$header = array(
'Return-path'=>'<'.$smtp['from'].'>',
'Date'=>date('r'),
'From'=> '<'.$smtp['from'].'>',
'MIME-Version'=>'1.0',
'Subject'=>inlineCode($smtp['subject']),
'To'=>$smtp['to'],
'Content-Type'=>'text/html; charset=UTF-8; format=flowed',
'Content-Transfer-Encoding'=>'base64'
);
$data = buildHeader($header).$CRLF.chunk_split(base64_encode($smtp['body']));
$content = "EHLO ".$smtp["url"].$CRLF; // 先得hello一下
$content .= "AUTH LOGIN".$CRLF.base64_encode($smtp["username"]).$CRLF.base64_encode($smtp["password"]).$CRLF; // 驗證登陸
$content .= "MAIL FROM:".$smtp["from"].$CRLF; // 發件地址
$content .= "RCPT TO:".$smtp["to"].$CRLF; // 收件地址
$content .= "DATA".$CRLF.$data.$CRLF.".".$CRLF; // 發送內容
$content .= "QUIT".$CRLF; // 退出
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // curl接收返回資料
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $content);
$test = curl_exec($curl);
var_dump($test);
echo "<br/>\r\n";
var_dump($content);
// 結束
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.