PHP SMTP mail Bulk program source code

Source: Internet
Author: User
Tags explode trim


<?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);//Sleep 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 contents
$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 not complete";
}
Load an unavailable list of emails
if (! $errorNo) {
if (file_exists (Sleeping_email)) {
$sleepMails = file (Sleeping_email);
if (!empty ($sleepMails)) {

foreach ($sleepMails as $sleepMail) {
Analytical
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) {
Randomly loading SMTP server and SMTP user names and Passwords
$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) {
Connecting to an SMTP server by Phpmailer a letter
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;//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 = "Sunny Network";

$mail->ishtml (TRUE);

$mail->addaddress ($to);
$mail->subject = $subject;
$mail->body = $body;

if (! $mail->send ()) {

echo "message could is sent.";
$errorNo = 3;
$ERRORMSG = $mail->errorinfo;
} else {
echo "
Send to $to success use $FROMMAILRN ";
Exit
}
}
if (3 = $errorNo) {
Record information, this information address sleeps n minutes
$content = "$fromMail |". Time (). "RN";//email| current timestamp
File_put_contents (Sleeping_email, $content, file_append);
}
echo "
Error No ($errorNo) ". $ERRORMSG. "RN";
Exit
?>

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.