php邮件发送

 在php中发送邮件有两种方法。一种是用php自带的mailto()函数,但是这个需要额外的配置php.ini,还需要包含一个类,不易成功,还有就是兼容性不好。
另外一种是是用socket发送,这个似乎更麻烦,但是幸好有很多已经写好的类,只要调用即可相对而言就变简单了,兼容性也是极好的。下面是本人用的一个案例,写在下面了,希望可以帮到 有需要的朋友。
  mail.class.php:
 1 <?php  2 class smtp  3 {  4  

 5 var $smtp_port;  6 var $time_out;  7 var $host_name;  8 var $log_file;  9 var $relay_host;  10 var $debug;  11 var $auth;  12 var $user;  13 var $pass;  14 

 15 var $sock;  16  

 17 function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass)  18 {  19 $this->debug = FALSE;  20 $this->smtp_port = $smtp_port;  21 $this->relay_host = $relay_host;  22 $this->time_out = 30; //is used in fsockopen()

 23 #  24 $this->auth = $auth;//auth

 25 $this->user = $user;  26 $this->pass = $pass;  27 #  28 $this->host_name = "localhost"; //is used in HELO command

 29 $this->log_file = "";  30  

 31 $this->sock = FALSE;  32 }  33  

 34 function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")  35 {  36 $mail_from = $this->get_address($this->strip_comment($from));  37 $body = ereg_replace("(^|(\r\n))(\.)", "\1.\3", $body);  38 $header .= "MIME-Version:1.0\r\n";  39 if($mailtype=="HTML"){  40 $header .= "Content-Type:text/html\r\n";  41 }  42 $header .= "To: ".$to."\r\n";  43 if ($cc != "") {  44 $header .= "Cc: ".$cc."\r\n";  45 }  46 $header .= "From: $from<".$from.">\r\n";  47 $header .= "Subject: ".$subject."\r\n";  48 $header .= $additional_headers;  49 $header .= "Date: ".date("r")."\r\n";  50 $header .= "X-Mailer:By Redhat (PHP/".phpversion().")\r\n";  51 list($msec, $sec) = explode(" ", microtime());  52 $header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">\r\n";  53 $TO = explode(",", $this->strip_comment($to));  54 if ($cc != "") {  55 $TO = array_merge($TO, explode(",", $this->strip_comment($cc)));  56 }  57 if ($bcc != "") {  58 $TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));  59 }  60 $sent = TRUE;  61 foreach ($TO as $rcpt_to) {  62 $rcpt_to = $this->get_address($rcpt_to);  63 if (!$this->smtp_sockopen($rcpt_to)) {  64 $this->log_write("Error: Cannot send email to ".$rcpt_to."\n");  65 $sent = FALSE;  66 continue;  67 }  68 if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {  69 $this->log_write("E-mail has been sent to <".$rcpt_to.">\n");  70 } else {  71 $this->log_write("Error: Cannot send email to <".$rcpt_to.">\n");  72 $sent = FALSE;  73 }  74 fclose($this->sock);  75 $this->log_write("Disconnected from remote host\n");  76 }  77 return $sent;  78 }  79  

 80  

 81  

 82 function smtp_send($helo, $from, $to, $header, $body = "")  83 {  84 if (!$this->smtp_putcmd("HELO", $helo)) {  85 return $this->smtp_error("sending HELO command");  86 }  87 #auth

 88 if($this->auth){  89 if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {  90 return $this->smtp_error("sending HELO command");  91 }  92 if (!$this->smtp_putcmd("", base64_encode($this->pass))) {  93 return $this->smtp_error("sending HELO command");  94 }  95 }  96 #  97 if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">")) {  98 return $this->smtp_error("sending MAIL FROM command");  99 } 100 if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">")) { 101 return $this->smtp_error("sending RCPT TO command"); 102 } 103 if (!$this->smtp_putcmd("DATA")) { 104 return $this->smtp_error("sending DATA command"); 105 } 106 if (!$this->smtp_message($header, $body)) { 107 return $this->smtp_error("sending message"); 108 } 109 if (!$this->smtp_eom()) { 110 return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]"); 111 } 112 if (!$this->smtp_putcmd("QUIT")) { 113 return $this->smtp_error("sending QUIT command"); 114 } 115 return TRUE; 116 } 117 function smtp_sockopen($address) 118 { 119 if ($this->relay_host == "") { 120 return $this->smtp_sockopen_mx($address); 121 } else { 122 return $this->smtp_sockopen_relay(); 123 } 124 } 125 function smtp_sockopen_relay() 126 { 127 $this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."\n"); 128 $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out); 129 if (!($this->sock && $this->smtp_ok())) { 130 $this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."\n"); 131 $this->log_write("Error: ".$errstr." (".$errno.")\n"); 132 return FALSE; 133 } 134 $this->log_write("Connected to relay host ".$this->relay_host."\n"); 135 return TRUE;; 136 } 137  

138 function smtp_sockopen_mx($address) 139 { 140 $domain = ereg_replace("^.+@([^@]+)$", "\1", $address); 141 if (!@getmxrr($domain, $MXHOSTS)) { 142 $this->log_write("Error: Cannot resolve MX \"".$domain."\"\n"); 143 return FALSE; 144 } 145 foreach ($MXHOSTS as $host) { 146 $this->log_write("Trying to ".$host.":".$this->smtp_port."\n"); 147 $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out); 148 if (!($this->sock && $this->smtp_ok())) { 149 $this->log_write("Warning: Cannot connect to mx host ".$host."\n"); 150 $this->log_write("Error: ".$errstr." (".$errno.")\n"); 151 continue; 152 } 153 $this->log_write("Connected to mx host ".$host."\n"); 154 return TRUE; 155 } 156 $this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")\n"); 157 return FALSE; 158 } 159  

160 function smtp_message($header, $body) 161 { 162 fputs($this->sock, $header."\r\n".$body); 163 $this->smtp_debug("> ".str_replace("\r\n", "\n"."> ", $header."\n> ".$body."\n> ")); 164  

165 return TRUE; 166 } 167  

168 function smtp_eom() 169 { 170 fputs($this->sock, "\r\n.\r\n"); 171 $this->smtp_debug(". [EOM]\n"); 172  

173 return $this->smtp_ok(); 174 } 175  

176 function smtp_ok() 177 { 178 $response = str_replace("\r\n", "", fgets($this->sock, 512)); 179 $this->smtp_debug($response."\n"); 180  

181 if (!ereg("^[23]", $response)) { 182 fputs($this->sock, "QUIT\r\n"); 183 fgets($this->sock, 512); 184 $this->log_write("Error: Remote host returned \"".$response."\"\n"); 185 return FALSE; 186 } 187 return TRUE; 188 } 189 function smtp_putcmd($cmd, $arg = "") 190 { 191 if ($arg != "") { 192 if($cmd=="") $cmd = $arg; 193 else $cmd = $cmd." ".$arg; 194 } 195 fputs($this->sock, $cmd."\r\n"); 196 $this->smtp_debug("> ".$cmd."\n"); 197 return $this->smtp_ok(); 198 } 199 function smtp_error($string) 200 { 201 $this->log_write("Error: Error occurred while ".$string.".\n"); 202 return FALSE; 203 } 204 function log_write($message) 205 { 206 $this->smtp_debug($message); 207 if ($this->log_file == "") { 208 return TRUE; 209 } 210 $message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message; 211 if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) { 212 $this->smtp_debug("Warning: Cannot open log file \"".$this->log_file."\"\n"); 213 return FALSE;; 214 } 215 flock($fp, LOCK_EX); 216 fputs($fp, $message); 217 fclose($fp); 218 

219 return TRUE; 220 } 221 

222 function strip_comment($address) 223 { 224 $comment = "\([^()]*\)"; 225 while (ereg($comment, $address)) { 226 $address = ereg_replace($comment, "", $address); 227 } 228 

229 return $address; 230 } 231 

232 function get_address($address) 233 { 234 $address = ereg_replace("([ \t\r\n])+", "", $address); 235 $address = ereg_replace("^.*<(.+)>.*$", "\1", $address); 236 return $address; 237 } 238 function smtp_debug($message) 239 { 240 if ($this->debug) { 241 echo $message; 242 } 243 } 244 }

在文件中引入上面这个文件类,具体发送的代码如下:

 1   if(isset($_POST['mail'])){

 2                         $smtpserver = "smtp.163.com";//SMTP服务器  这里用的是163的

 3                         $smtpserverport =25;//SMTP服务器端口

 4                         $smtpusermail = "***********@163.com"; //SMTP服务器的用户邮箱  在163注册的邮箱账号

 5                         $smtpemailto = "***********@qq.com"; //发送给谁

 6                         $smtpuser = "[email protected]"; //SMTP服务器的用户帐号  

 7                         $smtppass = "***********"; //SMTP服务器的用户密码  在163注册的邮箱账号密码

 8                         $mailsubject = "Test Subject";//邮件主题

 9                         $mailbody = "尊敬的会员,".$_POST['user_name'].":<br />您的商品已成功兑换。订单号:".$_POST['orderId'].",运单号:".$_POST['exp                           ressId'].",快递公司:".$_POST['express_name']."。";//邮件内容

10                         $mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件

11                         $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.

12                         $smtp->debug = FALSE;//是否显示发送的调试信息

13                         if($smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype)){

14                             //邮件发送成功以后,设置为已经处理

15                             $sql="update dou_exchange set exchange_status=1 where id=:id";

16                             $stmt=$pdo->prepare($sql); 

17                             $stmt->bindParam(':id', $_POST['id']);

18                             $stmt->execute();

19                             echo "<script>alert('已发送成功兑换邮件!');</script>";

20                         }else{

21                             echo "发送邮件失败";

22                             exit;}

 

你可能感兴趣的:(邮件发送)