Custom Fields in PHPMailer

Posted on 2009-04-16
Medium Priority
Last Modified: 2013-12-12
Hi Experts

I am testing the following scipt and it is working very well. I am pulling the message body from an external php file but would like to be able to include the recipients first name and other personal details within each email body. eg. 'Dear ****' and 'Your username is *****'.

Would I need to include the $body tag with the loop as well or is there a better option?

Many thanks
$mail                = new PHPMailer();
$body                = file_get_contents('templates/dns.php');
$body                = eregi_replace("[\]",'',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth      = true;                  // enable SMTP authentication
$mail->SMTPKeepAlive = true;                  // SMTP connection will not close after each email sent
$mail->Host          = "domain.com"; // sets the SMTP server
$mail->Port          = 26;                    // set the SMTP port for the GMAIL server
$mail->Username      = "username"; // SMTP account username
$mail->Password      = "password";        // SMTP account password
$mail->SetFrom('info@domain.com', 'DOMAIN');
$mail->AddReplyTo('info@domain.com', 'DOMAIN');
$mail->Subject       = "Greetings From DOMAIN";
$query  = "SELECT name, email FROM emails";
$result = @MYSQL_QUERY($query);
while ($row = mysql_fetch_array ($result)) {
  $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
  $mail->AddAddress($row["email"], $row["full_name"]);
  if(!$mail->Send()) {
    echo "Mailer Error (" . str_replace("@", "&#64;", $row["email"]) . ') ' . $mail->ErrorInfo . '<br />';
  } else {
    echo "Message sent to :" . $row["full_name"] . ' (' . str_replace("@", "&#64;", $row["email"]) . ')<br />';
  // Clear all addresses and attachments for next loop

Open in new window

Question by:sirnutty1
LVL 39

Accepted Solution

Roger Baklund earned 2000 total points
ID: 24162285
Your query is "SELECT name, email FROM emails", but you use $row['full_name'] in the loop...

In the email body file, include some placeholders for the variables you need, for exaxmple $name and $full_name: "Dear $full_name".

Then within the loop, replace the placeholders with the corresponding values from the database:
$ThisBody = str_replace(

Open in new window


Author Comment

ID: 24163906
Many thanks cxr

I changed a few details with security in mind in order to post but the 'full_name' variable does exist in the original.

Sincere thanks for the solution to including DB details. Much simpler than the options I was beginning to consider!!


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question