E.Nathan E.Nathan - 2 months ago 17
PHP Question

Use array of emails and names from database in PHPMailer

I'm using PHPMailer to send an email from a form to all the emails in a MySQL database.

I've managed to get the form to send an email to each email but I'm now trying to use their name in the email sent to them meaning each email woud begin with "Hello, [their-name],".

I'm using an array to loop through the results of my database query and adding each email and name as

$mail->addAddress('email@email.com, 'name');


I've tried saving the variable
$name = $row['name'};
inside the while loop but it inputs the last name from the database into all the emails.

My question is: Is it possible to access the
'name'
part of the function and use it each email. Or is it better to take their name from the database itself? And if so how would I go about doing that?

Also, when the email sends to each recipient, it shows all of the recipients in the "To:" field. This would be pretty bad for privacy. Is there a way to prevent this?

I'm fairly new to PHP and MySQL still so explanatory answers would be greatly appreciated.

Please find my code below.



<?php

// Take message value from form
$message = preg_replace("/\r\n|\r/", "<br />", $_REQUEST['message']);
$message = trim($message);

// Error reporting on
error_reporting(E_STRICT | E_ALL);
date_default_timezone_set('Etc/UTC');

// Require PHPMailer
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;

$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent, reduces SMTP overhead
$mail->Port = 587;
$mail->Username = 'my@email.com';
$mail->Password = 'mypassword';
$mail->setFrom('myemail@email.com', 'Me');
$mail->addReplyTo('myemail@email.com', 'Me');
$mail->Subject = "Newsletter";
/* ----- Add address function below ----- */
// Require Database connection
require 'dbcon.php';

// SELECT email and name values from database
$query = "SELECT email, name FROM customers";

// Result = the database query
$result = $con->query($query);
// Get array of users email addresses and names.
while($row = $result->fetch_array())
{
// Add recipients from values found in database
$mail->addAddress($row["email"], $row["name"]);

}
/* ----- Add address function above ----- */

$mail->Body ="Hello, <br> <br> $message";
$mail->AltBody = $message;

if(!$mail->Send())
{
echo "Message could not be sent.";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}

print 'Sent!';

?>




Answer

You are currently setting up to send a single email with several recipients. For security and etiquette, you should address the email to yourself (outside the loop):

$mail->addAddress('myemail@email.com', 'Me');

...and then add all of the recipients as BCC (inside the loop):

$mail->addBCC($row["email"], $row["name"]);

As for the name field. Add a debug line inside your loop and take a look at your output. Is the name field the same each time?

print "Adding recipient: {$row['email']} {$row['name']}";

If you want to sent several emails, one to each recipient, you need to instantiate a new email inside the loop each time.

[UPDATE]

If you want to send a single message to each recipient, then instantiate a new email each time (inside your loop):

<?php

// Take message value from form
$message = preg_replace("/\r\n|\r/", "<br />", $_REQUEST['message']);
$message = trim($message);

// Error reporting on
error_reporting(E_STRICT | E_ALL);
date_default_timezone_set('Etc/UTC');

// Require PHPMailer
require 'PHPMailer/PHPMailerAutoload.php';

// Require Database connection
require 'dbcon.php';

// SELECT email and name values from database
$query = "SELECT email, name FROM customers";

// Result = the database query
$result = $con->query($query);
// Get array of users email addresses and names.
while($row = $result->fetch_array())
{

    // Instantiate a NEW email
    $mail = new PHPMailer;

    // Set the email settings
    $mail->isSMTP();
    $mail->Host = 'smtp.example.com';
    $mail->SMTPAuth = true;
    $mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent, reduces SMTP overhead
    $mail->Port = 587;
    $mail->Username = 'my@email.com';
    $mail->Password = 'mypassword';
    $mail->setFrom('myemail@email.com', 'Me');

    $mail->addReplyTo('myemail@email.com', 'Me');
    $mail->Subject = "Newsletter";

    // Add recipient from values found in database
    $mail->addAddress($row["email"], $row["name"]);

    $mail->Body ="Hello, <br> <br> $message";
    $mail->AltBody = $message;

    if(!$mail->Send())
    {
    echo "Message could not be sent.";
    echo "Mailer Error: " . $mail->ErrorInfo;
    exit;
    }

    print "Sent mail to: {$row["email"]}, {$row["name"]}";

}


?>