php tutorial mass mailing (phpmailer send qq mail)
* @ author ray
* @ since 2009-08-07
smtp mail account must be more, otherwise it will be qq server as spam.
Have proper sleep
smtp.dat file format is
smtp.163.com | sss@163.com | password
smtp.sina.com | www@sina.com | password www.jzread.com
The program randomly pick a connection, send mail, if the send is unsuccessful, the mail address is stored in sleepmail.dat dormant for 30 minutes after sending (this is to connect smtp server to send unsuccessful changes).
* /
define ('__ debug__', false);
define ('__ ps tutorial w_file__', dirname (__ file__). '/smtp.dat');
define ('sleeping_email', dirname (__ file__). "/sleepmail.dat");// Hibernate email
define ('sleeping_time', 1800); // How long to sleep, 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 ();
// Recipient and mail title and mail 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 is incomplete";
}
// Load list of unavailable emails
if (! $ errorno) {
if (file_exists (sleeping_email)) {
$ sleepmails = file (sleeping_email);
if (! empty ($ sleepmails)) {
foreach ($ sleepmails as $ sleepmail) {
// resolve
if (false! == strpos ($ sleepmail, '|')) {
$ tmp = explode ('|', $ sleepmail);
if (isset ($ tmp [0]) && isset ($ tmp [1])) {
$ mail = trim ($ tmp [0]);
$ time = trim ($ tmp [1]);
//it's usable or not
if (($ currtime - $ time) <sleeping_time) {
$ unusemails [] = $ mail;
}
}
}
}
}
}
}
if (! $ errorno) {
Random loading smtp server and smtp user name 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 it is not available in the list,
if (! isset ($ smtpserver) ||! isset ($ frommail) ||! isset ($ psw)) {
$ errorno = 2;
$ errormsg = "did not find the sender qq mail and password";
}
}
if (! $ errorno && __debug__) {
echo "smtp: $ smtpserver from: $ frommail psw: $ psw user: $ smtpusernamern";
}
if (! $ errorno) {
/ / Phpmailer smtp server to send letters
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; / / www.jzread.com used to show the specific smtp error
$ 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 day network";
$ mail-> ishtml (true);
$ mail-> addaddress ($ to);
$ mail-> subject = $ subject;
$ mail-> body = $ body;
if (! $ mail-> send ()) {
// echo "message could not be sent.";
$ errorno = 3;
$ errormsg = $ mail-> errorinfo;
} else {
echo "
send to $ to success use $ frommailrn ";
exit
}
}
if (3 == $ errorno) {
// record information, the 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