PHP SMTP mail Bulk program source code

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags code content echo email file info mail password




<?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;


?>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.