troubleshooting Question

Could you have a look at this PHP script using PHPMailer to see its correction?

Avatar of Eduardo Fuerte
Eduardo FuerteFlag for Brazil asked on
PHP
25 Comments4 Solutions287 ViewsLast Modified:
Hi Experts

Could you have a look at this PHP script using PHPMailer to see its correction?

After some tests with a small number of address it run well.
But then, when using the complete base - about 750 address it seens to be erratic!

f.e.  - My own email address had received 03 copies of the same email, after a while my SMTP provider warned me that I reached the limit of email sendings I could send in 01 hour.


<?php

   error_reporting(E_STRICT | E_ALL);

   date_default_timezone_set('Etc/UTC');

   $host = "mysql.xxxxxxxxxx.com.br"; // Host KingHost

   $usermysql = "XXXXXXXX"; // Mysql username
   $passmysql = "XXXXXXXX"; // Mysql password
   $db_name = "XXXXXXXX"; // Database name locaweb
   $admin_user = "XXXXXXXX"; // Administrative user

   require '../PHPMailerAutoload.php';

   $mail = new PHPMailer;

   $mail->isSMTP();
   $mail->Host = 'smtp.xxxxxxxxxx.net';
   $mail->SMTPAuth = true;
   $mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent, reduces SMTP overhead
   $mail->Port = 25;

   $mail->Username = 'contato@xxxxxxxxxxxxx.com.br';
   $mail->Password = 'cipava01';


   $mail->setFrom('contato@xxxxxxxxxxx.com.br', 'Espiriplug - Gestao de Bibliotecas Espiritas');
   $mail->Subject = "eSpiriplug - Boletim 021 - Lancamento BETA do Sistema de Gestao de Bibliotecas Espiritas";
   $mail->AltBody = 'Para ver a mensagem use um leitor de email compatível com HTML!';


   //Connect to the database and select the recipients from your mailing list that have not yet been sent to
   //You'll need to alter this to match your database
   $mysql = mysqli_connect($host, $usermysql, $passmysql, $db_name);


   mysqli_select_db($mysql, 'mydb');

// Just for the test... ok!
//   $result = mysqli_query($mysql, "SELECT username , email , realname FROM forum_user WHERE substring(username,1,5)='teste'
//   OR   username ='eduardofuerte'");

   $result = mysqli_query($mysql, "SELECT username , email , realname FROM forum_user");

   foreach ($result as $row)
   { //This iterator syntax only works in PHP 5.4+

       $mail->addAddress($row['email'], $row['realname']);

       $body = "<header>";
       $body = "<font face=Arial size=2>";
       $body = $body ."<p>";
       $body = "<b> Prezado(a) ".$row['username'] . "<b> </br>";
       $body = $body . "</br>";
       $body = $body . "</header>";
       $body = $body . file_get_contents('contents_espiriplug.html');

       //Same body for all messages, so set this before the sending loop
       //If you generate a different body for each recipient (e.g. you're using a templating system),
       //set it inside the loop
       $mail->msgHTML($body);
       //msgHTML also sets AltBody, but if you want a custom one, set it afterwards


       if (!$mail->send())
       {
           echo "Mailer Error (" . str_replace("@", "&#64;", $row["email"]) . ') ' . $mail->ErrorInfo . '<br />';
           
           // Não parar de enviar.
           //break; //Abandon sending
           
           
       } else
       {
           echo "Message sent to :" . $row['username'] . ' (' . str_replace("@", "&#64;", $row['email']) . ')<br />';
           //Mark it as sent in the DB
           //mysqli_query($mysql, "UPDATE mailinglist SET sent = true WHERE email = '" . mysqli_real_escape_string($mysql, $row['email']) . "'");
       }
       // Clear all addresses and attachments for next loop
       $mail->clearAddresses();
       $mail->clearAttachments();
   }

Thanks in advance!
SOLUTION
Andy M
IT Systems Manager

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 4 Answers and 25 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 4 Answers and 25 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros