Dynamic Content for PHP MAILER – for GMAIL

If you are using PHP MAILER class and tired of the same old HTML static blah! blah content. Me and my officemate (give credit…) configured how we can use the same class but passing a dynamic content to that.

function mailer($rdate,$first, $last, $middlename, $emailadd, $message, $username, $password, $ReplyTo, $From, $FromName, $Subject, $AddressTo, $cc){
//include(“class.smtp.php”); // optional, gets called from within class.phpmailer.php if not already loaded

$mail             = new PHPMailer();

$body  = “This is where you can put the dynamic content $variables a lot of $variables”

$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = “ssl”;                 // sets the prefix to the servier
$mail->Host       = “smtp.gmail.com”;      // sets GMAIL as the SMTP server
$mail->Port       = 465;                   // set the SMTP port for the GMAIL server

$mail->Username   = $username;  // GMAIL username
$mail->Password   = $password;            // GMAIL password

$mail->AddReplyTo($ReplyTo,”John Doe”);

$mail->From       = $From;
$mail->FromName   = $FromName;

$mail->Subject    = $Subject;

//$mail->Body       = “Hi,<br>This is the HTML BODY<br>”;                      //HTML Body
$mail->AltBody    = “To view the message, please use an HTML compatible email viewer!”; // optional, comment out and test
$mail->WordWrap   = 50; // set word wrap


$mail->AddAddress($AddressTo, “John Doe”);


$mail->AddAttachment(“images/phpmailer.gif”);             // attachment

$mail->IsHTML(true); // send as HTML

if(!$mail->Send()) {
echo “Mailer Error: ” . $mail->ErrorInfo;
} else {
echo “Message sent!”;

This is the actual code that I am using, the variables are defined for its self as you can see. Save this file into something that you are familiar with. When you call this function use this.

$variable = $_REQUEST[‘textboxname’];

……and the rest of your dynamic content

mailer($receivedate, $firstname, $lastname, $mname, $email, $msg, “youraccount@gmail.com”,”PASSWORD”,”FROM@YOURDOMAIN.com”,”YOUR@YOURDOMAIN.COM”,”Name”,”SUBJECT “, “TOYOURACCOUNT@ACCOUNT.COM”,”CCyouraccount@gmail.com”);

just fill in the blanks and your done.

The red code the: $body; is where you will place the body of the email, TABLE or DIV whatever you want name it. Just put it there, and a several tests to see if your email is neat and beautiful.

Hope this helps on some of your projects. Happy CODING!


4 thoughts on “Dynamic Content for PHP MAILER – for GMAIL

  1. I am not quite sure I understand clearly but I believe this is not spam anyway!
    Please translate this in more generic readable format.

    Thanks dude!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s