php中通过curl smtp发送邮件_PHP
程序员文章站
2022-04-29 19:46:12
...
先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.
要发送的内容(这里的相关的规范有很多)
.
2.6.0 Queued mail for delivery
QUIT
2.0.0 Service closing transmission channel
Connection closed by foreign host.
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'=>'',
'Date'=>date('r'),
'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 "
\r\n";
var_dump($content);
// 结束
curl_close($curl);
以上只是测试的php
包测试+修改花了近6个小时让产品的代码兼容了fsockopen和curl
以后有空写个兼容fsockopen和curl简单发送邮件的smtp类
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
要发送的内容(这里的相关的规范有很多)
.
2.6.0 Queued mail for delivery
QUIT
2.0.0 Service closing transmission channel
Connection closed by foreign host.
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'=>'',
'Date'=>date('r'),
'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 "
\r\n";
var_dump($content);
// 结束
curl_close($curl);
以上只是测试的php
包测试+修改花了近6个小时让产品的代码兼容了fsockopen和curl
以后有空写个兼容fsockopen和curl简单发送邮件的smtp类