Link to home
Start Free TrialLog in
Avatar of sirnutty1
sirnutty1

asked on

Custom Fields in PHPMailer

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

ASKER CERTIFIED SOLUTION
Avatar of Roger Baklund
Roger Baklund
Flag of Norway image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sirnutty1
sirnutty1

ASKER

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!!