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.
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("@", "@", $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("@", "@", $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)
?>