Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php mailer and html includes with php variables

Tags:

php

mailer

Hello I am trying to send html emails using php mailer class. The problem is i would like to incllude php variables in my email while using includes as to keep things organized. Heres my php mailer....

 $place = $data['place'];
 $start_time = $data['start_time'];

$mail->IsHTML(true);    // set email format to HTML
$mail->Subject = "You have an event today";
$mail->Body = file_get_contents('../emails/event.html');
$mail->Send(); // send message

my question is, is it possible to have php variables in event.html ? i tried this with no luck (below is event.html)..

<table width='600px' cellpadding='0' cellspacing='0'>
<tr><td bgcolor='#eeeeee'><img src='logo.png' /></td></tr>
<tr><td bgcolor='#ffffff'  bordercolor='#eeeeee'>
<div style='border:1px solid #eeeeee;font-family:Segoe UI,Tahoma,Verdana,Arial,sans-serif;padding:20px 10px;'>
<p style=''>This email is to remind you that you have an upcoming meeting at $place on $start_time.</p>
<p>Thanks</p>
</div>
</td></tr>
</table>
like image 459
tytyguy Avatar asked Jul 14 '11 04:07

tytyguy


People also ask

What is the use of PHPMailer?

PHPMailer is a code library and used to send emails safely and easily via PHP code from a web server. Sending emails directly via PHP code requires a high-level familiarity to SMTP standard protocol and related issues and vulnerabilities about Email injection for spamming.

Does PHPMailer work on localhost?

The way you have setup your PHPMailer, it would require an SMTP server running on your localhost to send the messages. If you don't have an SMTP server running on your localhost, then you can use an external SMTP server to relay the messages through.

Is PHPMailer secure?

PHPMailer doesn't create/use any SQL itself, nor does it have anything to do with javascript, so it's secure on those fronts.


1 Answers

Couple ways to do it:

Token Template

<p> Some cool text %var1%,, %var2%,etc...</p>

Token Mailer

$mail->Body = strtr(file_get_contents('path/to/template.html'), array('%var1%' => 'Value 1', '%var2%' => 'Value 2'));

Buffer Template

<p> Some cool text $var1,, $var2,etc...</p>

Buffer Mailer

$var1 = 'Value 1';
$var2 = 'Value 2';
ob_start();
include('path/to/template.php');
$content = ob_get_clean();
$mail->Body = $content;
like image 72
prodigitalson Avatar answered Sep 28 '22 17:09

prodigitalson