?
Solved

Custom Fields in PHPMailer

Posted on 2009-04-16
2
Medium Priority
?
382 Views
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
<?php
 
//error_reporting(E_ALL);
error_reporting(E_STRICT);
 
date_default_timezone_set('Europe/London');
 
require_once('class.phpmailer.php');
 
$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";
 
@MYSQL_CONNECT("localhost","root","password");
@mysql_select_db("tables");
$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->MsgHTML($body);
  $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
  $mail->ClearAddresses();
  $mail->ClearAttachments();
}
?>

Open in new window

0
Comment
Question by:sirnutty1
2 Comments
 
LVL 39

Accepted Solution

by:
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(
  array('$name','$full_name'),
  array($row['name'],$row['full_name']),$body);
$mail->MsgHTML($ThisBody);

Open in new window

0
 

Author Comment

by:sirnutty1
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!!

0

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