php中通过curl smtp发送邮件的例子

WBOY
Lepaskan: 2016-07-25 09:05:18
asal
956 orang telah melayarinya
  1. /**
  2. 功能:邮件发送
  3. url: http://bbs.it-home.org
  4. */
  5. header("content-type:text/html;charset=utf-8");
  6. $smtp = array(
  7. "url" => "邮箱SMTP服务器地址",
  8. "port" => "邮箱SMTP服务器端口", // 一般为25
  9. "username" => "用户名",
  10. "password" => "密码",
  11. "from" => "发件地址",
  12. "to" => "收件地址",
  13. "subject" => "测试一下标题",
  14. "body" => "测试一下内容"
  15. );
  16. $CRLF = "\r\n";
  17. $test = "";
  18. $curl = curl_init();
  19. curl_setopt($curl, CURLOPT_URL, $smtp['url']);
  20. curl_setopt($curl, CURLOPT_PORT, $smtp['port']);
  21. curl_setopt($curl, CURLOPT_TIMEOUT,10);
  22. function inlineCode($str){
  23. $str = trim($str);
  24. return $str?'=?UTF-8?B?'.base64_encode($str).'?= ':'';
  25. }
  26. function buildHeader($headers){
  27. $ret = '';
  28. foreach($headers as $k=>$v){
  29. $ret.=$k.': '.$v."\n";
  30. }
  31. return $ret;
  32. }
  33. //
  34. $header = array(
  35. 'Return-path'=>'',
  36. 'Date'=>date('r'),
  37. 'From'=> '',
  38. 'MIME-Version'=>'1.0',
  39. 'Subject'=>inlineCode($smtp['subject']),
  40. 'To'=>$smtp['to'],
  41. 'Content-Type'=>'text/html; charset=UTF-8; format=flowed',
  42. 'Content-Transfer-Encoding'=>'base64'
  43. );
  44. $data = buildHeader($header).$CRLF.chunk_split(base64_encode($smtp['body']));
  45. $content = "EHLO ".$smtp["url"].$CRLF; // 先得hello一下
  46. $content .= "AUTH LOGIN".$CRLF.base64_encode($smtp["username"]).$CRLF.base64_encode($smtp["password"]).$CRLF; // 验证登陆
  47. $content .= "MAIL FROM:".$smtp["from"].$CRLF; // 发件地址
  48. $content .= "RCPT TO:".$smtp["to"].$CRLF; // 收件地址
  49. $content .= "DATA".$CRLF.$data.$CRLF.".".$CRLF; // 发送内容
  50. $content .= "QUIT".$CRLF; // 退出
  51. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // curl接收返回数据
  52. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $content);
  53. $test = curl_exec($curl);
  54. var_dump($test);
  55. echo "
    \r\n";
  56. var_dump($content);
  57. // 结束
  58. curl_close($curl);
  59. ?>
复制代码

花了近6个小时让产品的代码兼容了fsockopen和curl;有时间再研究下兼容fsockopen和curl简单发送邮件的smtp类。



sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan