doulu8537 2013-03-22 16:29
浏览 29

优化phpmailer的邮件发送过程

I am trying to send mails using phpmailer and code is working fine. However when I see the CPU usage history in task manager I could see it is fluctuating rapidly between 0% to 100%. And RAM usage is constant around 4.65GB of 6GB.

I was able to send 500 emails in 15 minutes or so and I feel its bit slow because my CPU usage is fluctuating a lot, may be I am wrong in my assumption.

  1. Can anyone help me in optimizing max usage of my system and fasten the process?
  2. Also I have created a column in table email_sent to check how many mails have been sent and avoid any duplication but the code is not updating the database and giving up! (so it is commented out below).

    <?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)
    

    ?>

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 模型在y分布之外的数据上预测能力不好如何解决
    • ¥15 processing提取音乐节奏
    • ¥15 gg加速器加速游戏时,提示不是x86架构
    • ¥15 python按要求编写程序
    • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
    • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
    • ¥15 opencv图像处理,需要四个处理结果图
    • ¥15 无线移动边缘计算系统中的系统模型
    • ¥15 深度学习中的画图问题
    • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条