2

我正在尝试使用 phpmailer 发送邮件,并且代码运行良好。但是,当我在任务管理器中看到 CPU 使用历史记录时,我可以看到它在 0% 到 100% 之间快速波动。RAM 使用量保持在 6GB 的 4.65GB 左右。

我能够在 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)
    

    ?>

4

0 回答 0