首页 >后端开发 >php教程 >php中通过curl smtp发送邮件的例子

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

WBOY
WBOY原创
2016-07-25 09:05:18985浏览
  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类。



声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn