博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SMTP邮件处理---H_smtp.php
阅读量:6093 次
发布时间:2019-06-20

本文共 9544 字,大约阅读时间需要 31 分钟。

debug = false; //是否开启调试,只在测试程序时使用,正式使用时请将此行注释 $to = "yin273642232@163.com,yin273642232@163.com,273642232@qq.com"; //收件人 $subject = "你好"; //$body = "

这是一个用 叶绿辉香(叶网) 发邮件的测试。支持SMTP认证!

"; $soururl="service4.html"; $fsour = fopen($soururl, "r"); while ($contents = fgets($fsour, 4096)) { $body.=$contents; } if($body == ""){ echo "邮件内容空!"; exit; } $send=$H_smtp -> sendmail($to,$sender,$subject,$body,$mailtype); if($send==1){ echo "邮件发送成功"; }else{ echo "邮件发送失败
"; echo "原因:".$smtp->logs; }*************************************///Start Classclass H_smtp{ public $smtp_port; //邮件端口号 public $time_out; //超时时间 public $host_name; //主机名称 public $log_file; //日志文件名称 public $relay_host; //邮件服务器 public $debug; //调试 public $auth; // public $user; //用户名 public $pass; //密码 public $sender; //收件人 public $sock; // public $H_desce; //描述 public $H_ROOT_DIR; //文件地址路径 public function __construct(){ //当实例化一个对象的时候,这个对象的这个方法首先被调用 $this->debug = false; //默认不开启调试 $this->time_out = 600; //超时时间 $this->host_name = "localhost"; //主机名 $this->log_file = "log/".date("Y")."_".date("m")."_".date("d").".log"; $this->logs = "send Email Start:\n"; //记录 $this->sock = false; return ''; } public function __destruct(){ //当删除一个对象或对象操作终止的时候,调用该方法 return ''; } public function __get($key){ //当试图读取一个并不存在的属性的时候被调用 return '['.$key.'] Variable not find'; } public function __set($key,$val){ //当试图向一个并不存在的属性写入值的时候被调用 return '['.$key.'] Variable not find'; } public function __call($key,$args){ //当试图调用一个对象并不存在的方法时,调用该方法 return '['.$key.'] Function not find'; } public function __toString(){ //当打印一个对象的时候被调用 return $this -> H_desce(); } public function __clone(){ //当对象被克隆时,被调用 return "clone"; } public function H_desce(){ //返回描述 $this -> H_desce .= '类名:H_smtp-邮件处理;'; $this -> H_desce .= '函数:strip_comment($address),返回:去除非法字符,参数:$address-邮件地址;'; $this -> H_desce .= '函数:get_address($address),返回:获得Email,参数:$address-邮件地址;'; $this -> H_desce .= '函数:log_write($message),返回:写入日志,参数:$message-消息;'; $this -> H_desce .= '函数:smtp_sockopen($address),返回:打开sock 连接,参数:$address-邮件地址;'; $this -> H_desce .= '函数:smtp_sockopen_mx($address),返回:打开sock 连接,参数:$address-邮件地址;'; $this -> H_desce .= '函数:smtp_sockopen_relay(),返回:打开sock 连接,参数:无;'; $this -> H_desce .= '函数:smtp_ok(),返回:写结果,参数:无;'; $this -> H_desce .= '函数:smtp_message($header, $body),返回:开始发送头和内容信息结果,参数:$header-头信息.$body-内容;'; $this -> H_desce .= '函数:smtp_eom(),返回:发送的过程中,参数:无;'; $this -> H_desce .= '函数:smtp_putcmd($cmd, $arg=""),返回:发送关键信息,参数:$cmd-内容.$arg-其他;'; $this -> H_desce .= '函数:smtp_error($string),返回:错误,参数:$string-消息;'; $this -> H_desce .= '函数:smtp_send($helo, $from, $to, $header, $body = ""),返回:发送邮件,参数:$helo-招呼.$from-发件地址.$to-收件地址.$header-头信息.$body-正文;'; $this -> H_desce .= '函数:sendmail($to,$from,$subject="",$body="",$mailtype,$cc="",$bcc ="",$additional_headers=""),返回:准备发送邮件,参数:省略;'; $this -> H_desce .= '函数:smtp_debug($message),返回:调试的输出,参数:$message-消息;'; return $this -> H_desce; } public function strip_comment($address){ //去除非法字符 while (ereg("\([^()]*\)", $address)){ $address = ereg_replace("\([^()]*\)", "", $address); } return $address; } public function get_address($address){ //获得Email $address = ereg_replace("([ \t\r\n])+", "", $address); $address = ereg_replace("^.*<(.+)>.*$", "\1", $address); return $address; } public function log_write($message){ //写入日志 $this->logs .= $message; $this->smtp_debug($message); if ($this->log_file == ""){ return true; } $message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message; if (!($fp = @fopen($this->log_file, "a+"))){ $this->smtp_debug("Warning: Cannot open log file \"".$this->log_file."\"\n"); return false; } flock($fp, LOCK_EX); fputs($fp, $message); fclose($fp); return true; } public function smtp_sockopen($address){ //打开sock 连接 if ($this->relay_host == ""){ return $this->smtp_sockopen_mx($address); }else{ return $this->smtp_sockopen_relay(); } } public function smtp_sockopen_mx($address){ //打开sock 连接 $domain = ereg_replace("^.+@([^@]+)$", "\1", $address); if (!@getmxrr($domain, $MXHOSTS)){ $this->log_write("Error: Cannot resolve MX \"".$domain."\"\n"); return false; } foreach ($MXHOSTS as $host){ $this->log_write("Trying to ".$host.":".$this->smtp_port."\n"); $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out); if (!($this->sock && $this->smtp_ok())){ $this->log_write("Warning: Cannot connect to mx host ".$host."\n"); $this->log_write("Error: ".$errstr." (".$errno.")\n"); continue; } $this->log_write("Connected to mx host ".$host."\n"); return true; } $this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")\n"); return false; } public function smtp_sockopen_relay(){ //打开sock 连接 $this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."\n"); $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out); if(!($this->sock && $this->smtp_ok())){ $this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."\n"); $this->log_write("Error: ".$errstr." (".$errno.")\n"); return false; } $this->log_write("Connected to relay host ".$this->relay_host."\n"); return true; } public function smtp_ok(){ //写(返回结果) $response = str_replace("\r\n", "", fgets($this->sock, 512)); $this->smtp_debug($response."\n"); if (!ereg("^[23]", $response)){ fputs($this->sock, "QUIT\r\n"); fgets($this->sock, 512); $this->log_write("Error: Remote host returned \"".$response."\"\n"); return false; } return true; } public function smtp_message($header, $body){ //开始发送头和内容信息(返回结果) fputs($this->sock, $header."\r\n".$body); $this->smtp_debug("> ".str_replace("\r\n", "\n"."> ", $header."\n> ".$body."\n> ")); return true; } public function smtp_eom(){ //发送的过程中 fputs($this->sock, "\r\n.\r\n"); $this->smtp_debug(". [EOM]\n"); return $this->smtp_ok(); } public function smtp_putcmd($cmd, $arg=""){ //发送关键信息 $cmd = $arg != "" ? ($cmd=="" ? $arg : $cmd." ".$arg) : $cmd; fputs($this->sock, $cmd."\r\n"); $this->smtp_debug("> ".$cmd."\n"); $this->log_write($cmd.".\n"); return $this->smtp_ok(); } public function smtp_error($string){ //错误返回 $this->log_write("Error: Error occurred while ".$string.".\n"); return false; } public function smtp_send($helo, $from, $to, $header, $body = ""){ //发送邮件 if (!$this->smtp_putcmd("HELO", $helo)){ return $this->smtp_error("sending HELO command"); } if($this->auth){ if(!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))){ return $this->smtp_error("sending AUTH command"); } if(!$this->smtp_putcmd("", base64_encode($this->pass))){ return $this->smtp_error("sending AUTH command"); } } if(!$this->smtp_putcmd("MAIL", "FROM:<".$this->sender.">")){ return $this->smtp_error("sending MAIL FROM command"); } if(!$this->smtp_putcmd("RCPT", "TO:<".$to.">")){ return $this->smtp_error("sending RCPT TO command"); } if(!$this->smtp_putcmd("DATA")){ return $this->smtp_error("sending DATA command"); } if(!$this->smtp_message($header, $body)) { return $this->smtp_error("sending message"); } if(!$this->smtp_eom()){ return $this->smtp_error("sending
.
[EOM]"); } if (!$this->smtp_putcmd("QUIT")) { return $this->smtp_error("sending QUIT command"); } return true; } public function sendmail($to,$from,$subject="",$body="",$mailtype,$cc="",$bcc ="",$additional_headers=""){ //准备发送邮件 $mail_from = $this->get_address($this->strip_comment($from)); $body = ereg_replace("(^|(\r\n))(\.)", "\1.\3", $body); $header .= "MIME-Version:1.0\r\n"; $header .= $mailtype=="HTML"?"Content-Type:text/html\r\n":""; //$header .= "To: ".$to."\r\n"; /*************************************************************************************************/ $header2 .= $cc != ""?"Cc: ".$cc."\r\n":""; $header2 .= "From: ".$from."\r\n"; $header2 .= "Subject: ".$subject."\r\n"; $header2 .= $additional_headers; $header2 .= "Date: ".date("r")."\r\n"; $header2 .= "X-Mailer: 72e.net (PHP/".phpversion().")\r\n"; list($msec,$sec)=explode(" ", microtime()); $header2 .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">\r\n"; $TO = explode(",", $this->strip_comment($to)); if ($cc != ""){ $TO = array_merge($TO, explode(",", $this->strip_comment($cc))); } if ($bcc != ""){ $TO = array_merge($TO, explode(",", $this->strip_comment($bcc))); } $myheader=$header; foreach ($TO as $rcpt_to){ $header1 = "To: ".$rcpt_to."\r\n"; $header =$myheader.$header1.$header2; $rcpt_to = $this->get_address($rcpt_to); if (!$this->smtp_sockopen($rcpt_to)){ $this->log_write("Error: Cannot send email to ".$rcpt_to."\n"); $sent = 2; continue; } if($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)){ $this->log_write("E-mail has been sent to <".$rcpt_to.">\n"); $sent = 1; }else{ $this->log_write("Error: Cannot send email to <".$rcpt_to.">\n"); $sent = 3; } fclose($this->sock); $this->log_write("Disconnected from remote host\n\n"); $header=""; $Hresusent .= $Hresusent!="" ? "," : ""; $Hresusent .= $sent; } return $Hresusent; } public function smtp_debug($message){ //调试的输出 if ($this->debug){ echo $message; } }}//End Class?>

 

转载于:https://www.cnblogs.com/huangxiang/archive/2012/01/17/2324722.html

你可能感兴趣的文章
[转]无法安装MVC3,一直卡在vs10-kb2483190
查看>>
Codeforces 520B:Two Buttons(思维,好题)
查看>>
web框架-(二)Django基础
查看>>
Jenkins持续集成环境部署
查看>>
emoji等表情符号存mysql的方法
查看>>
ubuntu14.04中国源
查看>>
Excel到R中的日期转换
查看>>
网络层
查看>>
centos7没有ifconfig命令
查看>>
10-SAP PI开发手册-ERP发布服务供外围系统调用(RFC类型)
查看>>
cmd命令行查看windows版本
查看>>
城市三联动简单实例
查看>>
opencv边缘检测的入门剖析(第七天)
查看>>
Spring Boot☞ 使用Thymeleaf模板引擎渲染web视图
查看>>
mac本地搭建wordpress
查看>>
CSS3学习手记(2) 伪类选择器
查看>>
DPS首战鞍山
查看>>
Microsoft Sync Framework基础篇 2:Microsoft Sync Framework架构与运行时
查看>>
Git 常用命令大全
查看>>
Xilinx ISE 12.4的简单应用
查看>>