您好,欢迎访问一九零五行业门户网

php中通过curl smtp发送邮件_PHP教程

先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测试代码:
复制代码 代码如下:
邮箱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类
http://www.bkjia.com/phpjc/325390.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/325390.htmltecharticle先google了一下,发现很多问相关问题的但没有相关的解答,在phpclasses里也没有找到相关的类于是自己边看stmp的相关协议边开始尝试curl smtp协议...
其它类似信息

推荐信息