首頁  >  文章  >  後端開發  >  PHP發送電子郵件類

PHP發送電子郵件類

WBOY
WBOY原創
2016-07-25 08:45:291179瀏覽
  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 = "rn",$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:<'.$fromaddress.'>'.$this->ending);
  46. $rcp = $this->get_echo();
  47. fputs($this->ms,'rcpt to:<'.$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


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn