如何在php mailer中建立新连接(发送几封电子邮件后重新连接)?

如何在php mailer中建立新连接(发送几封电子邮件后重新连接)?,php,command-line-interface,phpmailer,command-line-arguments,Php,Command Line Interface,Phpmailer,Command Line Arguments,这是我的密码。 发送了几封电子邮件后,我无法使此邮件发送者重新连接。 就像我希望他们在发送3封电子邮件后重新连接一样 重新定位您的代码并检查计数,这样应该可以做到: <?php error_reporting(0); require_once('PHPMailerAutoload.php'); $get = file_get_contents($argv[1]); $j = explode("\r\n", $get); $count = 0; //init count here and d

这是我的密码。 发送了几封电子邮件后,我无法使此邮件发送者重新连接。 就像我希望他们在发送3封电子邮件后重新连接一样
重新定位您的代码并检查计数,这样应该可以做到:

<?php
error_reporting(0);
require_once('PHPMailerAutoload.php');
$get = file_get_contents($argv[1]);
$j = explode("\r\n", $get);
$count = 0; //init count here and default to 0
$mail = new PHPMailer(); // init mail here before loop
foreach ($j as $email) {

    if ($count > 2) { // once count has reached 3 or more close and reopen
        $mail->SmtpClose();

        $mail = new PHPMailer();
        $count = 0;//reset count
    }


    print "\n\t";


    $mail->IsSMTP();                       // telling the class to use SMTP

    $mail->SMTPDebug = 0;
    // 0 = no output, 1 = errors and messages, 2 = messages only.
    $reconnect = 3;
    $mail->SMTPAuth = true;                // enable SMTP authentication 
    $mail->SMTPSecure = "tls";              // sets the prefix to the servier
    $mail->Host = "smtp-relay.gmail.com";        // sets Gmail as the SMTP server
    $mail->Port = 587;                     // set the SMTP port for the GMAIL 
    $mail->SMTPKeepAlive = true;
    $mail->Username = "admin@example.com";  // Gmail username
    $mail->Password = "password";      // Gmail password
    $mail->SetFrom('admin@example.com', 'No Reply');
    $mail->SetLanguage('de', 'language/');
    $mail->Priority = 1;
    $mail->Encoding = '7bit';
    $mail->CharSet = 'windows-1250';
    $mail->Subject = 'crazy test';
    $mail->ContentType = 'text/html';
    //$mail->AddEmbeddedImage ("logo.jpg", "okgambar");
    $mail->IsHTML();

    $mail->Body = file_get_contents('new-2.txt');

    $nq = 0;
    $mail->AddAddress($email, 'ImakoChan');
    // you may also use this format $mail->AddAddress ($recipient);

    if (!$mail->Send()) {
        echo $error_message = "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo $error_message = "Successfully sent!";
    }
    $count++;
}

?>


这将起作用,但实际上不必完整地重新创建PHPMailer对象。更有效的方法是创建一个实例,在发送循环之前设置所有不变的内容,然后在循环中每次只设置不同的内容。调用
SmtpClose()
足以使其关闭连接,并且在下次调用
send()
时,它将自动重新打开连接。看看PHPMailer为example.ops提供的邮件列表示例,我有一个新问题。在smtp关闭之前,mailer一起发送邮件。不是一个接一个。为什么?