Sending php mail with html

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
truepal20032001
Forum Commoner
Posts: 27
Joined: Mon Jun 25, 2007 8:28 pm

Sending php mail with html

Post by truepal20032001 »

How can i send a mail as html format if the instance is this:

Code: Select all

<?php
$msg = "<a href=\"http//myurl\">URL</a>";

mail('mymail','sub',$msg,'FROM:'. 'Someone');

?>
i want it to look like this

URL

but it looks like this

Code: Select all

<a href=\"myurl\">URL</a>
GuitarheadCA
Forum Newbie
Posts: 20
Joined: Fri Jul 13, 2007 12:59 am

Post by GuitarheadCA »

You need to create an email with a MIME header; you cannot just use the mail() function. PEAR has a Mime_Mailer class that can be downloaded and implemented for the purpose. It's a lot more work than a plain text email, but I know of no way around it.
truepal20032001
Forum Commoner
Posts: 27
Joined: Mon Jun 25, 2007 8:28 pm

Post by truepal20032001 »

can you show me with some code
GuitarheadCA
Forum Newbie
Posts: 20
Joined: Fri Jul 13, 2007 12:59 am

Post by GuitarheadCA »

I'm not sure how familiar you are with Object Oriented Programming (all of the following depends on it), but it's never too early to learn....

The files/classes you need are available from download on PEAR here <http://pear.php.net/package/Mail_Mime/download>

You'll also need the base PEAR class available for download here <http://pear.php.net/package/PEAR/download>

Expand the tarballs, and line up the directories where you need them.

Inside the Mail_Mime directory is an example file, it shows you how to create the email. You access the functions with syntax such as

$mail->setHTML('your string of <strong>HTML</strong> here');


Once you get to that file, it is a little clearer, but it may just take some messing around. I definately think PHP should ship with a way to handle rich-text email internally.

I apologize that this is not simpler, I can't think of any other way to explain it apart from posting 10 pages of code.
truepal20032001
Forum Commoner
Posts: 27
Joined: Mon Jun 25, 2007 8:28 pm

Post by truepal20032001 »

couldn't you just use some headers?...

Code: Select all

$headers = "from: $x_email_from\n";
$headers .= "Content-Type: text/html; charset=us-ascii\n";
$headers .= "Content-Transfer-Encoding: 7bit";
or something like that?...
truepal20032001
Forum Commoner
Posts: 27
Joined: Mon Jun 25, 2007 8:28 pm

Post by truepal20032001 »

yea, sorry... tried it out myself and it was simple 3 lines of code... :D
GuitarheadCA
Forum Newbie
Posts: 20
Joined: Fri Jul 13, 2007 12:59 am

Post by GuitarheadCA »

Well screw the PHP cookbook, it taught me the long way to do it :(
Post Reply