Home  >  Article  >  Backend Development  >  PHP send email class

PHP send email class

WBOY
WBOYOriginal
2016-07-25 08:45:291121browse
  1. /***************************************************** *******************************
  2. Instructions for use:
  3. $m= new SendM('smtp server address', 'Account', 'Password', Port (int), Timeout retry time (int));
  4. $m->Send('Recipient Email', 'Subject', 'Email text');
  5. Usage example:
  6. $m= new SendM('smtp.yeah.net','testuser','testuserpwd',25,30);
  7. $m->Send('a@coolmr.com ', 'Test email', 'This is a test email for sending emails, thank you for your support');
  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


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn