php smtp郵件群發程式原始碼

來源:互聯網
上載者:User


<?php
/**
*通過phpmailer發送qq郵件
*@author ray
*@since 2009-08-07
*/
define('__DEBUG__', false);
define('__PSW_FILE__', dirname(__FILE__) . '/smtp.dat');
define('SLEEPING_EMAIL', dirname(__FILE__) . "/sleepMail.dat");//休眠的email
define('SLEEPING_TIME', 1800);//休眠多長時間,以秒為單位
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();
//收件者和郵件標題和郵件內容
$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 = "參數不全";
}
//載入停用email列表
if (!$errorNo) {
    if (file_exists(SLEEPING_EMAIL)) {
        $sleepMails = file(SLEEPING_EMAIL);
        if (!empty($sleepMails)) {
       
            foreach($sleepMails as $sleepMail) {
                //解析
                if (false !== strpos($sleepMail, '|')) {
                    $tmp = explode('|', $sleepMail);
                    if (isset($tmp[0]) && isset($tmp[1])) {
                        $mail = trim($tmp[0]);
                        $time = trim($tmp[1]);
                       
                        //是否可用
                        if ( ($currTime - $time )< SLEEPING_TIME) {
                            $unuseMails[] = $mail;
                        }
                    }
                }
            }
        }
    }
}
if (!$errorNo) {
    //隨機載入smtp伺服器和smtp使用者名稱和密碼
    $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 (!isset($smtpServer) || !isset($fromMail) || !isset($psw)) {
        $errorNo = 2;
        $errorMsg = "沒找到寄件者QQ信箱和密碼";
    }
}
if (!$errorNo && __DEBUG__) {
    echo "smtp:$smtpServer from:$fromMail psw:$psw user:$smtpUserNamern";
}
if (!$errorNo) {
    //通過phpmailer串連smtp伺服器發信
    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;//用於顯示具體的smtp錯誤
   
    $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 = "晴天網路";
   
    $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) {
    //記錄資訊,該資訊地址休眠N分鐘
    $content = "$fromMail|" . time() . "rn";//email|目前時間戳
    file_put_contents(SLEEPING_EMAIL, $content, FILE_APPEND);
}
echo "
Error No($errorNo) " . $errorMsg . "rn";
exit;
?>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.