欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

用pear自带的mail类库发邮件

程序员文章站 2022-05-31 19:25:54
...
用pear自带的mail类库发邮件,可以用pear install 命令来安装对应的库
  1. $body = "点我重新生成密码";
  2. sendMail_smtp("xxxxxxxx@qq.com",'测试',$body);
  3. function sendMail_smtp($smtpemailto,$mailsubject,$mailbody){
  4. //error_reporting(7);
  5. require_once 'Mail.php';
  6. require_once 'Mail/mime.php';
  7. $from = 'admin@xxx.com';
  8. $to = $smtpemailto;
  9. $password = 'xxxxxx';
  10. $mail_config=array(
  11. "host"=>"smtp.ym.163.com",
  12. "port"=>25,
  13. "auth"=>true,
  14. "username"=>$from,
  15. "password"=>$password,
  16. "from"=>$from,
  17. );
  18. $hdrs = array(
  19. 'From'=>$from,
  20. 'To' => $to, //收信地址
  21. 'Subject'=>$mailsubject
  22. );
  23. $mime = new Mail_mime();
  24. //$mime->setTXTBody($text);
  25. //添加附件
  26. //$mime->addHTMLImage('php.gif','image/gif','12345',true);
  27. $mime->_build_params['html_charset'] = "utf-8";//设置编码格式
  28. $mime->_build_params['head_charset'] = "utf-8";//设置编码格式
  29. $mime->setHTMLBody($mailbody);
  30. $body = $mime->get();
  31. $hdrs = $mime->headers($hdrs);
  32. $mail = Mail::factory('smtp',$mail_config);
  33. $succ = $mail->send($to,$hdrs,$body);
  34. if (PEAR::isError($succ))
  35. {
  36. //echo 'Email sending failed: ' . $succ->getMessage();
  37. $err = 'Email sending failed: ' . $succ->getMessage();
  38. $content = $to."\t".date('Y-m-d H:i:s')."\t ".$err." \r\n" ;
  39. }
  40. else
  41. {
  42. //$content = $to."\t".date('Y-m-d H:i:s')."\t Email sent succesfully \r\n" ;
  43. return true;
  44. }
  45. }
复制代码