2

I just upgraded from PHP mail() to PHPMailer and I love it, but I need some help. I had my mailer setup to send an email for each address in a database to maintain privacy in the to and bcc areas so no one knew who else was getting these emails.

After upgrading to PHPMailer my script works almost flawlessly but now when the email sends every email address in my database is listed in the to section. I would like to change it to where it is one email per address again.

Any ideas?

Full code:

require('/home/jollyrogerpcs/public_html/settings/globalVariables.php'); // Require login variables
require('/home/jollyrogerpcs/public_html/settings/mysqli_connect.php'); // Require mysqli connections
require('/home/jollyrogerpcs/public_html/scripts/php/class.phpmailer.php'); // Require PHPMailer script
require('/home/jollyrogerpcs/public_html/scripts/php/class.pop3.php'); // Require PHPMailer script
require('/home/jollyrogerpcs/public_html/scripts/php/class.smtp.php'); // Require PHPMailer script
mysqli_select_db($conn,"newsletterlist");
$query = "SELECT * FROM newsletterusers";
$result = mysqli_query($conn, $query);
$subject = str_ireplace(array("\r", "\n", '%0A', '%0D'), '', $_POST['subject']);
$message = str_ireplace(array("\r", "\n", '%0A', '%0D'), '', $_POST['body']);

// Begin PHPMailer SMTP Authentication
$mail = new PHPMailer();

$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "localhost";  // specify main and backup server
//$mail->Host = "a2plcpnl0099.prod.iad2.secureserver.net";  // specify main and backup server
//$mail->Port = 465;      // set the SMTP port for the server
//$mail->SMTPAuth = true;     // turn on SMTP authentication
//$mail->Username = "jesse@example.com";  // SMTP username
//$mail->Password = "*******"; // SMTP password
// Begin PHP Mailer Headers
$mail->From = "jesse@example.com";
$mail->FromName = "Jesse Elser | Jolly Roger PCS Owner/Operator";
$mail->AddReplyTo("jesse@example.com", "Jesse Elser | Jolly Roger PCS Owner/Operator");
$mail->Subject = $subject;
$mail->IsHTML(true);  // set email format to HTML

if (!$result) exit("The query did not succeded");
else {
    while ($row = mysqli_fetch_array($result)) {
        $to = $row['email'];
        $mail->AddAddress($to); 
        $encodedTo = rtrim(strtr(base64_encode($to), '+/', '-_'), '=');
        date_default_timezone_set("America/Chicago");
        $date = date("m/d/Y h:i:sa");
        $date .= " CST";
        $mail->Body ='<!DOCTYPE HTML>';
        $mail->Body .='<body style="padding: 0; margin: 0; background-color: #000; color: #fff; text-align: center; font-family: verdana;">';
        $mail->Body .='<div id="container" style="width: 90%; margin: 0 auto; text-align: left; background-color: #121212;">';
        $mail->Body .='<div id="header" style="border-bottom: 1px solid #ff6400;">';
        $mail->Body .='<img src="http://example.com/images/main/logo.png" width="100%">';
        $mail->Body .='</div>';
        $mail->Body .='<div id="subject" style="background-color: #121212; text-align: center;">';
        $mail->Body .='<h1 style="color: #ff6400; margin: 0;">'.$subject.'</h1>';
        $mail->Body .='</div>';
        $mail->Body .='<div id="message" style="background-color: #232323; color: #fff; padding: 10px;">';
        $mail->Body .=  $message;
        $mail->Body .='</div>';
        $mail->Body .='<div id="footer" style="background-color: #121212; padding: 10px;">';
        $mail->Body .='<a href="http://example.com" style="text-decoration: none; color: #ff6400;">Visit Our Site</a> | Thanks for subscribing to our newsletter! | <a href="http://example.com/scripts/php/unsubscribe.php?id='.$encodedTo.'" style="text-decoration: none; color: #ff6400;">Unsubscribe</a> <br> E-mail sent: ';
        $mail->Body .= $date;
        $mail->Body .='</div>';
        $mail->Body .='</body>';
    }
}
mysqli_close($conn);
if(!$mail->Send())
{
   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}

echo "Message has been sent";
header('Location: http://example.com/newsletter.php');
Jesse Elser
  • 974
  • 2
  • 11
  • 39
  • You've based your code on an old example and are probably using an old version of PHPMailer. [This example provided with PHPMailer](https://github.com/PHPMailer/PHPMailer/blob/master/examples/mailing_list.phps) does exactly what you need, and is also a fair bit more efficient. – Synchro Jul 27 '15 at 06:51
  • Hmmm I got it working with the answer provided, but as soon as I'm off mobile I'll check out your link. Thanks :) – Jesse Elser Jul 27 '15 at 16:30

1 Answers1

2

You create the mail object outside the while loop. Than you add an address with each iteration of the while loop and overwrite the content of "body" in each iteration. Meaning that the mail that is actually sent to all users, and only the latest "version" of "body" is sent. You have to send the mail from within the while loop, since each mail is unique per user (due to the unsubscribe link). You might also need to create the mail object from within the while loop! (unless you clear the recipiens: phpMailer - How do you Remove Recipients

$mailer->ClearAllRecipients( )

Personally I would keep track in the database which user has received a mail already. Also I would alter the SQL query to only get a limited amount of users i.e.

$query = "SELECT * FROM newsletterusers WHERE isMailed = 0 LIMIT 0,20";

Than I would run this code in a cronjob (lets say each minute) and send 20 mails per minute (or more/less) just to balance the load of the server over a larger period.

Community
  • 1
  • 1
Jeffrey
  • 1,766
  • 2
  • 24
  • 44
  • Hey it worked and I understand now :) Everything PHPMail related needs to be in the `while` loop. Thanks so much. – Jesse Elser Jul 25 '15 at 20:02