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

PHP发送电子邮件类

程序员文章站 2024-02-05 08:43:40
...
  1. /***********************************************************************************
  2. 使用说明:
  3. $m= new SendM('smtp服务器地址','账号','密码',端口(int),超时重试时间(int));
  4. $m->Send('收件人邮箱 ','主题','邮件正文内容');
  5. 使用范例:
  6. $m= new SendM('smtp.yeah.net','testuser','testuserpwd',25,30);
  7. $m->Send('a@coolmr.com ','测试邮件','这是一封邮件发送类的测试邮件,谢谢您的支持');
  8. *************************************************************************************/
  9. class SendM{
  10. private $Mailhost,$Mailuser,$Mailpwd,$Mailport,$Mailtimeout,$ms,$ending = "\r\n",$endingc="\n";
  11. function __construct($Mailhost,$Mailuser,$Mailpwd,$Mailport,$Mailtimeout){
  12. $this->Mailhost=$Mailhost;
  13. $this->Mailuser=$Mailuser;
  14. $this->Mailpwd=$Mailpwd;
  15. $this->Mailport=$Mailport;
  16. $this->Mailtimeout=$Mailtimeout;
  17. $this->ConnectSmtpServer();
  18. }
  19. private function ConnectSmtpServer(){
  20. if(!is_string($this->Mailhost)){ settype(trim($this->Mailhost),"string"); }
  21. if(!is_integer($this->Mailport)){ settype(trim($this->Mailport),"integer"); }
  22. if(!is_integer($this->Mailtimeout)){ settype(trim($this->Mailtimeout),"integer"); }
  23. $this->ms=@fsockopen($this->Mailhost,$this->Mailport,$this->errorno,$this->errorstr,$this->Mailtimeout);
  24. if(substr(PHP_OS,0,3) != "WIN"){ stream_set_timeout($this->ms, $this->Mailtimeout, 0);}
  25. $rcp = $this->get_echo();
  26. fputs($this->ms,"ehlo bobo".$this->ending);
  27. $rcp = $this->get_echo();
  28. if(substr($rcp,0,3)!='250'){ return false; }
  29. fputs($this->ms,'auth login'.$this->ending);
  30. $rcp = $this->get_echo();
  31. if(substr($rcp,0,3)=='334'){ $this->Auth($this->Mailuser,$this->Mailpwd); }else{ return false; } }
  32. private function Auth($Mailuser,$Mailpwd){
  33. $this->Mailuseren=base64_encode($Mailuser); $this->Mailpwden=base64_encode($Mailpwd);
  34. fputs($this->ms,$this->Mailuseren.$this->ending);
  35. $rcp = $this->get_echo();
  36. fputs($this->ms,$this->Mailpwden.$this->ending);
  37. $rcp = $this->get_echo(); }
  38. private function get_echo(){
  39. $edata=""; while($estr=@fgets($this->ms,600)){ $edata .= $estr;
  40. if(substr($estr,3,1) == " ") { break; } }
  41. return $edata; }
  42. public function Send($to,$subject,$connect){
  43. $host=explode('.',$this->Mailhost);
  44. $fromaddress=$this->Mailuser.'@'.$host[1].'.'.$host[2];
  45. fputs($this->ms,'mail from:'.$this->ending);
  46. $rcp = $this->get_echo();
  47. fputs($this->ms,'rcpt to:'.$this->ending);
  48. $rcp = $this->get_echo();
  49. fputs($this->ms,'data'.$this->ending);
  50. $rcp = $this->get_echo();
  51. fputs($this->ms,"to:$to".$this->endingc);
  52. fputs($this->ms,"from:$fromaddress".$this->endingc);
  53. fputs($this->ms,"subject:$subject".$this->endingc.$this->endingc);
  54. fputs($this->ms,"$connect".$this->endingc);
  55. fputs($this->ms,'.'.$this->ending);
  56. $rcp = $this->get_echo(); if(substr($rcp,0,3)=='250'){header("Location:main_pro.php?act=msg&errors=on&msg=邮件发送成功!已成功提交至对方服务器!"); }else{ header("Location:main_pro.php?act=msg&errors=on&msg=很遗憾,邮件发送失败了!请检查邮件账户配置是否正确!"); }
  57. }
  58. }
  59. ?>
复制代码

发送电子邮件, PHP