PHPmailer发送缓慢

时间:2018-10-09 13:15:46

标签: php performance phpmailer

我有时使用PHPmailer发送新闻通讯,但开始发送的速度很慢,可能要花几分钟的时间才能开始。

Web主机告诉我,每个发送的邮件之间我需要延迟3秒,所以我使用sleep(3);在代码中。

谁能看看我的代码,并给我一些建议,以使其更快地启动并加快速度?

<?php
error_reporting(E_STRICT | E_ALL);

date_default_timezone_set('Europe/Oslo');

require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

$mail->isSMTP();
$mail->Host = "cpanel32.webhost.no";
$mail->SMTPAuth = true;
$mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent, reduces SMTP overhead
$mail->Port = 26;
$mail->Username = "bjorn@websiteadress.com";
$mail->Password = "XXXXX";
$mail->setFrom("bjorn@websiteadress.com", "Sender name");
$mail->addReplyTo("bjorn@websiteadress.com", "Sender name");

$mail->Subject = "Forespørsel";

//Same body for all messages, so set this before the sending loop
//If you generate a different body for each recipient (e.g. you"re using a templating system),
//set it inside the loop
//Connect to the database and select the recipients from your mailing list that have not yet been sent to
//You"ll need to alter this to match your database
$mysql = mysqli_connect("xxx", "xxxx", "xxx");
mysqli_select_db($mysql, "xxx");
$result = mysqli_query($mysql, "SELECT * FROM mail_tester");

foreach ($result as $row) { 

    $body = file_get_contents('https://www.websiteadress.com/dm/sendmail/contents.html');
    $eposten = ($row["epost"]);
    $kilden = ($row["kilde"]);
    $body = str_replace('%testeposten%',$eposten,$body);
    $body = str_replace('%testkilden%',$kilden,$body);
    $mail->msgHTML($body);
    $mail->AltBody = "For å lese denne e-posten må du ha en HTML-kompatibel e-postleser eller se den på nettet på https://www.websiteadress.com/dm/nyhetsbrev.php To view the message, please use an HTML compatible email viewer, or read it online on https://www.websiteadress.com/dm/nyhetsbrev.php ";
    if (ob_get_level() == 0) ob_start();

    sleep(3); //flyttet sleep fra her
    $mail->addAddress($row['epost']);
    if (!$mail->send()) {
        echo "Mailer Error (" . str_replace("@", "&#64;", $row["epost"]) . ") " . $mail->ErrorInfo . "<br />";
        break; //Abandon sending
    } else {
        echo "&#10004; " . str_replace("@", "&#64;", $row["epost"]) . "";
        echo date(" H:i:s", time());
        echo "<br />";
    }

    {
            echo str_pad('',4096)."\n";    
            ob_flush();
            flush();
            ob_end_flush();
    }
    // Clear all addresses and attachments for next loop
    $mail->clearAddresses();
    $mail->clearAttachments();

    }
}
?>

0 个答案:

没有答案