* @ Author ray
* @ Since 2009-08-07
There must be many smtp mail accounts, otherwise they will be treated as spam by the qq server.
2. Sleep properly.
The smtp. dat file format is
Smtp.163.com | sss@163.com | password
Smtp.sina.com | www@sina.com | password www.111cn.net
The program randomly extracts a connection and sends an email. If the email fails to be sent, the email address is saved to sleepmail. send after dat sleep for 30 minutes (this is a modification made to fail to send after connecting to the smtp server multiple times ).
*/
| The code is as follows: |
Copy code |
Define ('_ debug _', false );
Define ('_ PS tutorial w_file _', dirname (_ file _). '/smtp. dat ');
Define ('Sleeping _ email ', dirname (_ file _). "/sleepmail. dat"); // dormant email
Define ('Sleeping _ time', 1800); // how long the sleep lasts, 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 email title and 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: $ subject ";
}
If (empty ($ mailfile) | empty ($ to) | empty ($ subject )){
$ Errorno = 1;
$ Errormsg = "incomplete parameters ";
}
// Load the list of unavailable emails
If (! $ Errorno ){
If (file_exists (sleeping_email )){
$ Sleepmails = file (sleeping_email );
If (! Empty ($ sleepmails )){
Foreach ($ sleepmails as $ sleepmail ){
// Resolution
If (false! = Strpos ($ sleepmail, '| ')){
$ Tmp = explode ('|', $ sleepmail );
If (isset ($ tmp [0]) & isset ($ tmp [1]) {
$ Mail = trim ($ tmp [0]);
$ Time = trim ($ tmp [1]);
// Available or not
If ($ currtime-$ time) <sleeping_time ){
$ Unusemails [] = $ mail;
}
}
}
}
}
}
}
If (! $ Errorno ){
// Load the smtp server and smtp username and password randomly
$ 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 the list is unavailable
If (! Isset ($ smtpserver) |! Isset ($ frommail) |! Isset ($ psw )){
$ Errorno = 2;
$ Errormsg = "the sender's qq mailbox and password are not found ";
}
}
If (! $ Errorno & _ debug __){
Echo "smtp: $ smtpserver from: $ frommail psw: $ psw user: $ smtpusername ";
}
If (! $ Errorno ){
// Use phpmailer to connect to the smtp server for Mail
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.111cn.net is 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 = "Sunday Network ";
$ Mail-> ishtml (true );
$ Mail-> addaddress ($ );
$ Mail-> subject = $ subject;
$ Mail-> body = $ body;
If (! $ Mail-> send ()){
// Echo "message cocould not be sent .";
$ Errorno = 3;
$ Errormsg = $ mail-> errorinfo;
} Else {
Echo"
Send to $ to success use $ frommail ";
Exit;
}
}
If (3 = $ errorno ){
// Record information. The address is dormant for n minutes.
$ Content = "$ frommail |". time (). ""; // email | CURRENT TIMESTAMP
File_put_contents (sleeping_email, $ content, file_append );
}
Echo"
Error no ($ errorno) ". $ errormsg ."";
Exit; |