Home  >  Article  >  Backend Development  >  [php] php curl smtp发送邮件

[php] php curl smtp发送邮件

WBOY
WBOYOriginal
2016-06-23 14:35:35899browse

公司的云平台把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 <CRLF>.<CRLF>要发送的内容(这里的相关的规范有很多).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 <?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类

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:linux安装phpNext article:[php]PHP错误处理