Custom Fields in PHPMailer

Posted on 2009-04-16
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          = ""; // 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('', 'DOMAIN');

$mail->AddReplyTo('', '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

    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

    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

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now