优化phpmailer的邮件发送过程

时间:2013-03-22 16:29:03

标签: optimization query-optimization phpmailer php

我正在尝试使用phpmailer发送邮件,代码工作正常。但是,当我在任务管理器中看到CPU使用历史记录时,我可以看到它在0%到100%之间快速波动。 RAM的使用率在4.65GB左右,大约为6GB。

我能够在15分钟左右发送500封电子邮件,我觉得它有点慢,因为我的CPU使用率波动很大,可能是我的错误。

  1. 任何人都可以帮我优化系统的最大使用量并加快处理过程吗?
  2. 此外,我在表email_sent中创建了一个列,以检查已发送的邮件数量并避免重复,但代码未更新数据库并放弃! (所以它在下面评论)。

    <?php
    //this code sends mails in HTML format using emailids from database. It also sends attachment if needed.
    error_reporting(E_ALL);
    //error_reporting(E_STRICT);
    ini_set("display_errors", "on");
    ini_set('max_execution_time', 30000000); //300 seconds = 5 minutes
    ini_set('memory_limit', '6500M'); //Maximum amount of memory a script may consume (128MB by default)
    date_default_timezone_set('UTC');
    
    require_once('../class.phpmailer.php');
    //include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
    
    $mail                = new PHPMailer();
    $body                = file_get_contents("contents.html");
    //$body                = preg_replace('/[\]/','',$body);
    $body                = preg_replace('/IDontKnowWTFThisFunctionIsdoing/','',$body);
    
    $mail->IsSMTP();        // telling the class to use SMTP
    $mail->Host          = "ssl://smtp.gmail.com";
    $mail->SMTPAuth      = true;                  // enable SMTP authentication
    $mail->SMTPKeepAlive = true;                  // SMTP connection will not close after each email sent
    $mail->Host          = "ssl://smtp.gmail.com"; // sets the SMTP server
    $mail->Port          = 465;                    // set the SMTP port for the GMAIL server
    $mail->Username      = "myemail@gmail.com"; // SMTP account username
    $mail->Password      = "password";        // SMTP account password
    //$mail->SetFrom    ('myemail@gmail.com', 'me');
    $mail-> From         = "myemail@gmail.com";
    $mail->FromName      = "me";    
    $mail->AddReplyTo   ('myemail@gmail.com', 'me');
    
    $mail->Subject       = "Subject";
    
    @MYSQL_CONNECT("localhost","root","");
    @mysql_select_db("database"); 
    $startnum1 = 501;
    $endnum1   = 5000;
    //$query     = "SELECT emailid FROM test WHERE email_sent = 0 Limit $startnum1,$endnum1";
    $query     = "SELECT emailid FROM test Limit $startnum1,$endnum1";
    $result = @MYSQL_QUERY($query);
    echo "message sending in process";
    while ($row = mysql_fetch_array ($result)) {
      $mail->body       = $body;
      $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
      $mail->IsHTML(true);
      $mail->MsgHTML($body);
      $mail->AddAddress($row["emailid"], $row["emailid"]);
      //$mail->AddStringAttachment($row["emailid"], "contents.html");
    
      if(!$mail->Send()) {
        echo "Mailer Error (" . str_replace("@", "&#64;", $row["emailid"]) . ') ' . $mail->ErrorInfo . '<br />';
      } else {
        //$query1 = "UPDATE test SET Email_Sent= 1 WHERE EmailID= emailid Limit $startnum1,$endnum1";
        //$result1 = @MYSQL_QUERY($query1);
        echo "Message sent to :" . $row["emailid"] . ' (' . str_replace("@", "&#64;", $row["emailid"]) . ')<br />';
      }
      // Clear all addresses and attachments for next loop
      $mail->ClearAddresses();
      //$mail->ClearAttachments();
    }
    [CPU history] (http://s3.postimg.org/v99ucpq6p/Capture.jpg)
    [System information] (http://s3.postimg.org/3n72s16tt/Capture1.jpg)
    

    &GT;

0 个答案:

没有答案