469,289 Members | 2,156 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,289 developers. It's quick & easy.

Using templates to send e-mail in PHP

I'm looking at sending e-mails from PHP based on templates with
expansion of variables, similar to a mail merge. Does anyone know of
existing code for this or can point me in the right direction. I
assume I will need to use regexps but am not yet sure exactly how.

Any advice gratefully recieved,
Geoff
Jul 17 '05 #1
5 15805
Geoff Soper wrote:
I'm looking at sending e-mails from PHP based on templates with
expansion of variables, similar to a mail merge. Does anyone know of
existing code for this or can point me in the right direction. I
assume I will need to use regexps but am not yet sure exactly how.

Any advice gratefully recieved,
Geoff


Hi Geoff,

I would advise you to just roll your own.
First try to send an email (to yourself) with PHP. If it works fine, start
messing with the bodycontent.

If you want to send to a load of people stored in a database, just loop
though the resultset and create a seperate email for everyone in the
resultset.

You can use a template with placeholders.
example:

// databasequery

for (...) {

$db_fullname = $row["fullname"];
$db_email = $row["email"];

$message = "\n";
$message .= "Dear ".$db_fullname.",\n";
$message .= "\n";
$message .= "How do you like our email?\n";
$message .= "\n";
$message .= "Regards,\nGeoff\n";

$subject = "My newsletter.";

mail ($db_email, $subject, $message);

}
Should work.

Regards,
Erwin

Jul 17 '05 #2
Hello Erwin / Geoff,

Erwin Moller wrote:
Geoff Soper wrote:
I'm looking at sending e-mails from PHP based on templates with
expansion of variables, similar to a mail merge. Does anyone know of
existing code for this or can point me in the right direction. I
assume I will need to use regexps but am not yet sure exactly how.
You can use a template with placeholders.


Besides the example of Erwin, you could also use the include
statement to include your mail template, which would be a
normal php file ( HTML with php code embedded ) and use
ob_start and the other ob_* functions to catch the output
of the mail template.

Advantage of this approach is that your templates can
be HTML with some placeholders and you don't have to
play with concatenation of strings containing HTML
with lots of escape sequences a.s.o.

Best regards,

Eric Veltman
Jul 17 '05 #3
Hello,

On 01/26/2004 10:51 AM, Geoff Soper wrote:
I'm looking at sending e-mails from PHP based on templates with
expansion of variables, similar to a mail merge. Does anyone know of
existing code for this or can point me in the right direction. I
assume I will need to use regexps but am not yet sure exactly how.


You may want to look at this class. It comes with an example of sending
personalized mailings to multiple receipients, using Smarty engine to
compose messages based on templates. This is faster than doing regular
expression based replacements of placeholders.

The example shows how to compose personalized HTML messages with
alternative and also personalized text parts that are viewed only on
mail programs that do not support HTML. There are also separate HTML and
text templates for use in this example with Smarty.

http://www.phpclasses.org/mimemessage

--

Regards,
Manuel Lemos

Free ready to use OOP components written in PHP
http://www.phpclasses.org/

MetaL - XML based meta-programming language
http://www.meta-language.net/

Jul 17 '05 #4
ge*****************@alphaworks.co.uk (Geoff Soper) wrote in message news:<8e**************************@posting.google. com>...
I'm looking at sending e-mails from PHP based on templates with
expansion of variables, similar to a mail merge. Does anyone know of
existing code for this or can point me in the right direction. I
assume I will need to use regexps but am not yet sure exactly how.

Any advice gratefully recieved,


My taste...

$from_email = 'fetched from DB or so';
$to_email = 'fetched from DB or so';
$name = 'fetched from DB or so';
//send via mail template...
include('./includes/mailtemplates/send_welcome_msg.inc.php');
<?php
//send_welcome_msg.inc.php
//mail template file.... May use some other codes to edit this
template file
$subject = 'xxxxxxx';
$message = <<<EOT
Hiiii $name,

xxxxx

Webmaster
EOT;
//send email...
mail($to_email, $subject, $message,
"From: Webmaster <$from_email>\r\n" .
"Reply-To: $from_email\r\n" .
"X-Mailer: PHP/" . phpversion()
);
?>
--
"I don't believe in the God who doesn't give me food, but shows me
heaven!" -- Swami Vivekanandha
Email: rrjanbiah-at-Y!com
Jul 17 '05 #5
R. Rajesh Jeba Anbiah wrote:

<?php
//send_welcome_msg.inc.php
//mail template file.... May use some other codes to edit this
template file
$subject = 'xxxxxxx';
$message = <<<EOT
Hiiii $name,

xxxxx

Webmaster
EOT;
//send email...


(Off topic)

Thanks Rajesh,

You just did me huge favor. :-)
I was really missing my Perl here-documents, and for some silly reason I
didn't expect to find them in PHP, but they are just there!
You just showed me. :-)
Thanks.

Regards,
Erwin Moller

Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by Sebastian Faust | last post: by
4 posts views Thread by Pat Turner | last post: by
2 posts views Thread by Bore Biko | last post: by
4 posts views Thread by n.phelge | last post: by
4 posts views Thread by qning88 | last post: by
3 posts views Thread by tech | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.