Using PHPMailer, emails are successful. Then my script inserts the email content into mysqli database.
Most of the time this query insert fails. But sometimes the query insert does works.
Using function my_error_handler ($e_number, $e_message, $e_file, $e_line, $e_vars)
I can see the error code which is: mysqli::ping(): send of 5 bytes failed with errno=32 Broken pipe
.
(The database insert query is wrapped inside if($con->ping()){...}
.)
Having read (in stackoverflow) what 'broken pipe' means, I do not know how to correct this error. Any help please. Thanks for your response.
For information, PHPMailer takes about 1 minute to execute. Inside the PHPMailer loop are only calls to $mail->Body
, $mail->AltBody
, $mail->addAddress()
, $mail->send()
and $mail->clearAddresses()
(after sending).
Here is the code:
$mail = new PHPMailer(true); //Argument true in constructor enables exceptions
$mail->isHTML(true);
$mail->isSMTP(true);
$mail->Host = "$smtpHost";
$mail->Port = "$smtpPort";
$mail->Username = "$username"; // SMTP username
$mail->Password = "$password"; // SMTP password
$mail->SMTPKeepAlive = true; //SMTP connection will not close after each email sent,
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->SMTPDebug = 0;
$realfrom=$username;
$mail->From = "$realfrom";
$mail->FromName = "$sendname";
$mail->addReplyTo("$username", "Admin");
$mail->Subject = "$subject";
$mail->AddEmbeddedImage('../../Images/bannersm2.jpg', 'banner', 'JC');
$body ="....."; // edit $body
//for each subscriber
while ($row = mysqli_fetch_array($Qemails, MYSQLI_ASSOC)){ // all members from TempTable
$First = $row['First_Name'];
$Last = $row['Last_Name'];
$email = $row['email'];
$emailaddress = $email;
$to = $email;
$name=($row['addressto']);
$site_name = "www.jc.net"; // Your website name
//Edit the body of the message to be sent here
$body = "Hello $First".$body;
$mail->Body = "<img alt=\"PHPMailer\" src=\"cid:banner\"><br><br>$body";
$mail->AltBody = "$body";
$mail->addAddress("$to", "$name");//To address and name
try {
$mail->send();
print ("<div id=\"sectionContent\">");
print ("<font color=\"red\"><img src=../../Images/checkmark.jpg> Successful.</font><br><br>");
print ("</div><br>");
$count++;
echo"<font class='F088'>";
echo "This email makes a cummulative total of $count messages sent.<br>";
echo"</font>";
} catch (Exception $e) {
$fail = $fail +1;
echo "Mailer Error: " . $mail->ErrorInfo;
print ("<div id=\"sectionContent\">");
echo"<font class='F088'>";
print ("<br><br><font color=\"red\"><img src=Images/crossMark.jpg> Uh-Oh! Something.</font>");
print ("</font></div>");
echo"<font class='F088'>";
echo "This email makes a cummulative total of $fail messages NOT sent.</font>";
}
$mail->clearAddresses();//Clear all addresses and attachments (only if different) for the next iteration
}
if($count>=1){
$sent=$count + $sent;
}
// Connect to the database is $con
/* check if server is alive */
if ($con->ping()) {
$subject1 = mysqli_real_escape_string($con, $subject);
$message1 = $_POST["message"];
$message1 = mysqli_real_escape_string($con, $message1);
$sendname1= mysqli_real_escape_string($con, $sendname);
$queryarchive = "INSERT INTO archived_groupemails (groupname, subject, message, sendname, datesent, numberemails) VALUES ( '$selectedgroupsl', '$subject1', '$message1', '$sendname1', NOW(), '$sent') ";
$resultarchive = @mysqli_query($con ,$queryarchive); // Run the query.
}
Based on advice from my server, I edited the .htaccess for the whole website. No help.
php_value upload_max_filesize 256M
php_value post_max_size 256M
php_value memory_limit 512M
php_value max_input_time 180
php_value max_execution_time 180
I contacted the server who referred me to stackoverflow.