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