使用代碼實現郵箱發送

  1 <?php
  2 
  3 class Smtp
  4 {
  5     /* Public Variables */
  6     public $smtp_port;
  7 
  8     public $time_out;
  9 
 10     public $host_name;
 11 
 12     public $log_file;
 13 
 14     public $relay_host;
 15 
 16     public $debug;
 17 
 18     public $auth;
 19 
 20     public $user;
 21 
 22     public $pass;
 23 
 24     /* Private Variables */
 25     private $sock;
 26 
 27     /* Constractor */
 28     function __construct($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass)
 29     {
 30         $this->debug = FALSE;
 31 
 32         $this->smtp_port = $smtp_port;
 33 
 34         $this->relay_host = $relay_host;
 35 
 36         $this->time_out = 30; //is used in fsockopen()
 37 
 38 
 39         $this->auth = $auth;//auth
 40 
 41         $this->user = $user;
 42 
 43         $this->pass = $pass;
 44 
 45 
 46         $this->host_name = "localhost"; //is used in HELO command
 47         $this->log_file = "";
 48 
 49         $this->sock = FALSE;
 50 
 51     }
 52 
 53     /* Main Function */
 54 
 55     function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")
 56     {
 57 
 58         $mail_from = $this->get_address($this->strip_comment($from));
 59 
 60         $body = preg_replace("/(^|(\r\n))(\.)/", "\1.\3", $body);
 61 
 62         $header = "MIME-Version:1.0\r\n";
 63 
 64         if($mailtype=="HTML"){
 65 
 66             $header .= "Content-Type:text/html\r\n";
 67 
 68         }
 69 
 70         $header .= "To: ".$to."\r\n";
 71 
 72         if ($cc != "") {
 73 
 74             $header .= "Cc: ".$cc."\r\n";
 75 
 76         }
 77 
 78         $header .= "From: $from<".$from.">\r\n";
 79 
 80         $header .= "Subject: ".$subject."\r\n";
 81 
 82         $header .= $additional_headers;
 83 
 84         $header .= "Date: ".date("r")."\r\n";
 85 
 86         $header .= "X-Mailer:By Redhat (PHP/".phpversion().")\r\n";
 87 
 88         list($msec, $sec) = explode(" ", microtime());
 89 
 90         $header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">\r\n";
 91 
 92         $TO = explode(",", $this->strip_comment($to));
 93 
 94         if ($cc != "") {
 95 
 96             $TO = array_merge($TO, explode(",", $this->strip_comment($cc)));
 97 
 98         }
 99 
100         if ($bcc != "") {
101 
102             $TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));
103 
104         }
105 
106         $sent = TRUE;
107 
108         foreach ($TO as $rcpt_to) {
109 
110             $rcpt_to = $this->get_address($rcpt_to);
111 
112             if (!$this->smtp_sockopen($rcpt_to)) {
113 
114                 $this->log_write("Error: Cannot send email to ".$rcpt_to."\n");
115 
116                 $sent = FALSE;
117 
118                 continue;
119 
120             }
121 
122             if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {
123 
124                 $this->log_write("E-mail has been sent to <".$rcpt_to.">\n");
125 
126             } else {
127 
128                 $this->log_write("Error: Cannot send email to <".$rcpt_to.">\n");
129 
130                 $sent = FALSE;
131 
132             }
133 
134             fclose($this->sock);
135 
136             $this->log_write("Disconnected from remote host\n");
137 
138         }
139 
140         return $sent;
141 
142     }
143 
144     /* Private Functions */
145 
146     function smtp_send($helo, $from, $to, $header, $body = "")
147     {
148 
149         if (!$this->smtp_putcmd("HELO", $helo)) {
150 
151             return $this->smtp_error("sending HELO command");
152 
153         }
154 
155         //auth
156         if($this->auth){
157 
158             if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {
159 
160                 return $this->smtp_error("sending HELO command");
161 
162             }
163 
164             if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
165 
166                 return $this->smtp_error("sending HELO command");
167 
168             }
169 
170         }
171 
172 
173         if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">")) {
174 
175             return $this->smtp_error("sending MAIL FROM command");
176 
177         }
178 
179         if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">")) {
180 
181             return $this->smtp_error("sending RCPT TO command");
182 
183         }
184 
185         if (!$this->smtp_putcmd("DATA")) {
186 
187             return $this->smtp_error("sending DATA command");
188 
189         }
190 
191         if (!$this->smtp_message($header, $body)) {
192 
193             return $this->smtp_error("sending message");
194 
195         }
196 
197         if (!$this->smtp_eom()) {
198 
199             return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]");
200 
201         }
202 
203         if (!$this->smtp_putcmd("QUIT")) {
204 
205             return $this->smtp_error("sending QUIT command");
206 
207         }
208 
209         return TRUE;
210 
211     }
212 
213     function smtp_sockopen($address)
214     {
215 
216         if ($this->relay_host == "") {
217 
218             return $this->smtp_sockopen_mx($address);
219 
220         } else {
221 
222             return $this->smtp_sockopen_relay();
223 
224         }
225 
226     }
227 
228     function smtp_sockopen_relay()
229     {
230 
231         $this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."\n");
232 
233         $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
234 
235         if (!($this->sock && $this->smtp_ok())) {
236 
237             $this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."\n");
238 
239             $this->log_write("Error: ".$errstr." (".$errno.")\n");
240 
241             return FALSE;
242 
243         }
244 
245         $this->log_write("Connected to relay host ".$this->relay_host."\n");
246 
247         return TRUE;
248 
249     }
250 
251     function smtp_sockopen_mx($address)
252     {
253 
254         $domain = preg_replace("/^.+@([^@]+)$/", "\1", $address);
255 
256         if (!@getmxrr($domain, $MXHOSTS)) {
257 
258             $this->log_write("Error: Cannot resolve MX \"".$domain."\"\n");
259 
260             return FALSE;
261 
262         }
263 
264         //专注与php学习 http://www.daixiaorui.com 欢迎您的访问
265 
266         foreach ($MXHOSTS as $host) {
267 
268             $this->log_write("Trying to ".$host.":".$this->smtp_port."\n");
269 
270             $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
271 
272             if (!($this->sock && $this->smtp_ok())) {
273 
274                 $this->log_write("Warning: Cannot connect to mx host ".$host."\n");
275 
276                 $this->log_write("Error: ".$errstr." (".$errno.")\n");
277 
278                 continue;
279 
280             }
281 
282             $this->log_write("Connected to mx host ".$host."\n");
283 
284             return TRUE;
285 
286         }
287 
288         $this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")\n");
289 
290         return FALSE;
291 
292     }
293 
294     function smtp_message($header, $body)
295     {
296 
297         fputs($this->sock, $header."\r\n".$body);
298 
299         $this->smtp_debug("> ".str_replace("\r\n", "\n"."> ", $header."\n> ".$body."\n> "));
300 
301         return TRUE;
302 
303     }
304 
305     function smtp_eom()
306     {
307 
308         fputs($this->sock, "\r\n.\r\n");
309 
310         $this->smtp_debug(". [EOM]\n");
311 
312         return $this->smtp_ok();
313 
314     }
315 
316     function smtp_ok()
317     {
318 
319         $response = str_replace("\r\n", "", fgets($this->sock, 512));
320 
321         $this->smtp_debug($response."\n");
322 
323         if (!preg_match("/^[23]/", $response)) {
324 
325             fputs($this->sock, "QUIT\r\n");
326 
327             fgets($this->sock, 512);
328 
329             $this->log_write("Error: Remote host returned \"".$response."\"\n");
330 
331             return FALSE;
332 
333         }
334 
335         return TRUE;
336 
337     }
338 
339     function smtp_putcmd($cmd, $arg = "")
340     {
341 
342         if ($arg != "") {
343 
344             if($cmd=="") $cmd = $arg;
345 
346             else $cmd = $cmd." ".$arg;
347 
348         }
349 
350         fputs($this->sock, $cmd."\r\n");
351 
352         $this->smtp_debug("> ".$cmd."\n");
353 
354         return $this->smtp_ok();
355 
356     }
357 
358     function smtp_error($string)
359     {
360 
361         $this->log_write("Error: Error occurred while ".$string.".\n");
362 
363         return FALSE;
364 
365     }
366 
367     function log_write($message)
368     {
369 
370         $this->smtp_debug($message);
371 
372         if ($this->log_file == "") {
373 
374             return TRUE;
375 
376         }
377 
378         $message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;
379 
380         if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {
381 
382             $this->smtp_debug("Warning: Cannot open log file \"".$this->log_file."\"\n");
383 
384             return FALSE;
385 
386         }
387 
388         flock($fp, LOCK_EX);
389 
390         fputs($fp, $message);
391 
392         fclose($fp);
393 
394 
395         return TRUE;
396 
397     }
398 
399 
400     function strip_comment($address)
401     {
402 
403         $comment = "/\([^()]*\)/";
404 
405         while (preg_match($comment, $address)) {
406 
407             $address = preg_replace($comment, "", $address);
408 
409         }
410 
411         return $address;
412 
413     }
414 
415 
416     function get_address($address)
417     {
418 
419         $address = preg_replace("/([ \t\r\n])+/", "", $address);
420 
421         $address = preg_replace("/^.*<(.+)>.*$/", "\1", $address);
422 
423         return $address;
424 
425     }
426 
427     function smtp_debug($message)
428     {
429 
430         if ($this->debug) {
431 
432             echo $message;
433 
434         }
435 
436     }
437 
438 }
 1 <?php
 2     // 端口:
 3     // 110:接受邮件服务器
 4     // 25:发送邮件服务器  
 5     // smtp:个人邮件   
 6     // smtp.163.com
 7 
 8 
 9 
10 
11     require_once "Smtp.class.php";
12     //******************** 配置信息 ********************************
13     $smtpserver = "smtp.163.com";//SMTP服务器
14     $smtpserverport =25;//SMTP服务器端口
15     $smtpusermail = "[email protected]";//SMTP服务器的用户邮箱
16     $smtpemailto = "[email protected]";//发送给谁
17     $smtpuser = "[email protected]";//SMTP服务器的用户帐号,注:部分邮箱只需@前面的用户名
18     $smtppass = "";//SMTP服务器的用户密码
19     $mailtitle = "小貓咪  你最近不乖呀!";//邮件主题
20     $mailcontent = "<h1>什麼玩意!</h1>";//邮件内容
21     $mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件
22     //************************ 配置信息 ****************************
23     $smtp = new Smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
24     $smtp->debug = false;//是否显示发送的调试信息
25     $state = $smtp->sendmail($smtpemailto, $smtpusermail, $mailtitle, $mailcontent, $mailtype);
26 
27     echo "<div style='width:300px; margin:36px auto;'>";
28     if($state==""){
29         echo "对不起,邮件发送失败!请检查邮箱填写是否有误。";
30         echo "<a href='index.html'>点此返回</a>";
31         exit();
32     }
33     echo "恭喜!邮件发送成功!!";
34     echo "<a href='index.html'>点此返回</a>";
35     echo "</div>";
36 
37 ?>

猜你喜欢

转载自www.cnblogs.com/songbao/p/11237753.html