sending a .PHP page as HTML using phpmailer

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
HEMOglobina
Forum Newbie
Posts: 6
Joined: Tue Aug 09, 2005 7:23 pm

sending a .PHP page as HTML using phpmailer

Post by HEMOglobina »

I'm using phpmailer to send newsletter from a company to its employees. The contents of the newsletter are read from a database and inserted in a template page using PHP.
The problem that I'm facing is that I have coded the entire template page inside the page that sends the newsletter. It is something like this:

Code: Select all

$html = "<html><head><title>Newsletter</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"></head>";
$html .= "<body bgcolor=\"#FFFFFF\" text=\"#000000\" link=\"#000000\" vlink=\"#000000\" alink=\"#000000\" leftmargin=\"0\" topmargin=\"15\" marginwidth=\"0\" marginheight=\"0\"><font face=\"Arial, Helvetica, sans-serif\"><div align=\"center\">";
$html .= "etc etc etc";

// somewhere else in the code:
$mail = new PHPMailer();
$mail->Body = $html;
$mail->IsHTML(true);
...
$mail->Send();
This is very cumbersome... Every change I have to make to the newsletter is hell. I have to build it in a separated file (for previewing purpouse) and then bring it inside the page that sends the newsletter (escaping every quote and other things).

There has got to be an easier way of doing this. Any help is greatly appreciated.
Thanks a lot,
HEMOglobina

PS: The page that I want to send is full of PHP commands that reads from the DB... just in case this matters.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

search around about template engines.. things such as Smarty and Prado will be run across.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

I highly suggest smarty for just such a combo. I use it with phpmailer, and its a killer combo.
HEMOglobina
Forum Newbie
Posts: 6
Joined: Tue Aug 09, 2005 7:23 pm

Post by HEMOglobina »

I just found out that you need administrator privilegies to install Smarty... The ISP where I'm running this website don't have Smarty installed and I dont have administator privilegies... I guess my excitement didnt last long :)

[short lasting excitement]
WOW! I just took a look at smarty. It looks very useful! I still have no clue on how it could help me on this specific problem (It fells like using a missile to shoot down a mosquito), but I will definetly take the time to try it out.
Thank you for pointing it out. Roja, would you recommend me some tutorials for it?
[/short lasting excitement]

Thanks a lot,
HEMOglobina
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

HEMOglobina wrote:I just found out that you need administrator privilegies to install Smarty... The ISP where I'm running this website don't have Smarty installed and I dont have administator privilegies... I guess my excitement didnt last long :)
You can install it just like any other php app. No need for admin priv's.
HEMOglobina wrote:[short lasting excitement]
I still have no clue on how it could help me on this specific problem (It fells like using a missile to shoot down a mosquito), but I will definetly take the time to try it out.
Think of smarty as a clean/clear seperator for output from code. You don't do any echo, print, anything like that. You just have a smarty template, and you have smarty do the output. Simple!

For your problem, you build a template for the email, and have smarty dump the translated template into a string, which you then send. Simple!
HEMOglobina wrote: Thank you for pointing it out. Roja, would you recommend me some tutorials for it?
[/short lasting excitement]
Google for it.. its used by Yahoo, and hundreds of other sites.. its extremely well documented, and lots of tutorials exist for it.
HEMOglobina
Forum Newbie
Posts: 6
Joined: Tue Aug 09, 2005 7:23 pm

Post by HEMOglobina »

Thank you Roja. My fault here. I stopped reading the documentation when I got to the line that says that you have to make changes to your php.ini...
I just installed it and it is working 100% (I guess... I dont know how to use it enough to try other features).
I'll google it for tutorials.
Best regards,
HEMOglobina
Post Reply