一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

php 邮件群发(phpmailer发送qq邮件)

时间:2022-06-24 22:08:14 编辑:袖梨 来源:一聚教程网

*@author ray
*@since 2009-08-07

smtp邮件帐号一定要多,不然会被qq服务器当作垃圾邮件。
2.得适当的休眠

smtp.dat文件格式为
smtp.163.com|[email protected]|密码
smtp.sina.com|[email protected]|密码 www.111com.net
程序随机抽取一个连接,发送邮件,如果发送不成功,将该邮件地址存入sleepmail.dat休眠30分后在发送(这个是为了连接smtp服务器多次后发送不成功而做的修改)。
*/

 代码如下 复制代码
define('__debug__', false);
define('__ps教程w_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:$subject ";
}
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:$smtpusername ";
}
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;//www.111com.net用于显示具体的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 $frommail ";
        exit;
    }
}
if (3 == $errorno) {
    //记录信息,该信息地址休眠n分钟
    $content = "$frommail|" . time() . " ";//email|当前时间戳
    file_put_contents(sleeping_email, $content, file_append);
}
echo "
error no($errorno) " . $errormsg . " ";
exit;

热门栏目