Last Update:2017-09-14
Source: Internet
Author: User
Keywords
Web Programming
PHP Tutorials
<?php /** * Send QQ mail via Phpmailer * @author Ray * @since 2009-08-07 */ define (' __debug__ ', false); define (' __psw_file__ ', DirName (__file__). '/smtp.dat '); define (' Sleeping_email ', DirName (__file__). "/sleepmail.dat");/Dormant Email define (' Sleeping_time ', 1800);//hibernate for how long, in seconds define (' File_append ', 1); if (!function_exists (' file_put_contents ')) { function file_put_contents ($n, $d, $flag = False) { $mode = ($flag = = File_append | | strtoupper ($flag) = = ' File_append ')? ' A ': ' W '; $f = @fopen ($n, $mode); if ($f = = False) { return 0; } else { if (Is_array ($d)) $d = implode ($d); $bytesWritten = fwrite ($f, $d); fclose ($f); return $bytesWritten; } } } $errorNo = 0; $errorMsg = '; $currTime = time (); $unuseMails = Array (); //Recipients and message headers and message content $to = Isset ($argv [1])? $ARGV [1]: ""; $subject = Isset ($argv [2])? $ARGV [2]: ""; $mailFile = Isset ($argv [3])? $ARGV [3]: ""; if (__debug__) { echo " file: $mailFile to: $to Subject: $SUBJECTRN "; } if (Empty ($mailFile) | | | empty ($to) | | empty ($subject)) { $errorNo = 1; $ERRORMSG = "parameter incomplete"; } //Load unavailable Email list if (! $errorNo) { if (file_exists (Sleeping_email)) { $sleepMails = file (Sleeping_email); if (!empty ($sleepMails)) { foreach ($sleepMails as $sleepMail) { Parsing if (False!== Strpos ($sleepMail, ' | ')) { $tmp = explode (' | ', $sleepMail); if (isset ($tmp [0]) && isset ($tmp [1])) { $mail = Trim ($tmp [0]); $time = Trim ($tmp [1]); //Is available if (($currTime-$time) < Sleeping_time) { $unuseMails [] = $mail; } } } } } } } if (! $errorNo) { //Random load SMTP server and SMTP username and password $info = file (__psw_file__); $len = count ($info); do { $rnd = Mt_rand (0, $len-1); $line = isset ($info [$rnd]) $info [$rnd]: ""; if (False!== Strpos ($line, ' | ')) { $tmp = explode (' | ', $line); if (isset ($tmp [0]) && isset ($tmp [1]) && isset ($tmp [2])) { $smtpServer = Trim ($tmp [0]); $fromMail = Trim ($tmp [1]); $PSW = Trim ($tmp [2]); $smtpUserName = substr ($fromMail, 0, Strrpos ($fromMail, ' @ ')); } } }while (In_array ($fromMail, $unuseMails))//If in the unavailable list, load at the secondary if (!isset ($smtpServer) | | |!isset ($fromMail) | |!isset ($PSW)) { $errorNo = 2; $ERRORMSG = "did not find the sender QQ mailbox and password"; } } if (! $errorNo && __debug__) { echo "SMTP: $smtpServer from: $fromMail PSW: $PSW User: $smtpUserNamern"; } if (! $errorNo) { //Connect to SMTP server via Phpmailer require (dirname (__file__). "/phpmailer/class.phpmailer.php"); Require (DirName (__file__). "/phpmailer/class.smtp.php"); $mail = new Phpmailer (); $body = $mail->getfile ($mailFile); $body = Eregi_replace ("[]", ", $body); //charset $mail->charset = "GB2312"; //$mail->smtpdebug = 2;//used to display specific SMTP errors $mail->issmtp (); $mail->smtpauth = true; if ("smtp.qq.com" = Trim ($smtpServer)) { $mail->username = $fromMail; } else { $mail->username = $smtpUserName; } $mail->password = $PSW; $mail->host = $smtpServer; $mail->from = $fromMail; $mail->fromname = "Sunny Network"; $mail->ishtml (true); $mail->addaddress ($to); $mail->subject = $subject; $mail->body = $body; if (! $mail->send ()) { //echo "message could not is sent." $errorNo = 3; $ERRORMSG = $mail->errorinfo; } else { echo " Send to $to success use $FROMMAILRN "; exit; } } if (3 = $errorNo) { //record information, this information address sleep n minutes $content = "$fromMail |". Time (). "RN";//email| current timestamp file_put_contents (Sleeping_email, $content, file_append); } echo " Error No ($errorNo) ". $ERRORMSG. "RN"; exit; ?>