trying to create html code within the body of a mail message

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
jwcrosby
Forum Newbie
Posts: 8
Joined: Mon Sep 29, 2008 4:51 pm

trying to create html code within the body of a mail message

Post by jwcrosby »

I'm working on a routine that emails a message and the message has to have html in it. I want to include an html link that also includes a variable in it. Below is what I have, but it isn't working. I know it's probably a matter of the single and/or double quote placement.

The html link should end up being: http://www.domain.org/reference_tool-php/form.php?k=424 (the number at the end being the $LinkID variable--I included a random number here). My code is showing the proper text on the screen in the email as received, but the actual hyperlink is not picking up the variable at the end. How can this be corrected to work properly?

Here's how I'm setting the body string:
$body = 'Here is your link: <a href=""http://www.domain.org/reference_tool-ph ... D.'</a></p>';

The email message is being sent with the following code ($to and $subject are defined elsewhere):
mail($to,$subject,$body,
"To: $RefFull\n" .
"From: $from\n" .
"MIME-Version: 1.0\n" .
"Content-type: text/html; charset=iso-8859-1");

Thanks in advance for your help.

Jerry
jwcrosby
Forum Newbie
Posts: 8
Joined: Mon Sep 29, 2008 4:51 pm

Re: trying to create html code within the body of a mail message

Post by jwcrosby »

Well, I solved it myself and I'll post the solution to possibly help others. I ended up parsing the email message body even further.

First, I set a variable that would include the quotes in the output:
$url="\"http://www.domainname.com/reference_too ... ?k=$LinkID\"";

Then I put it back together as I created the message variable:

$body = $body.'Here is your link: <a href=';
$body = $body.$url;
$body = $body .'>http://www.domainname.com/reference_too ... D.'</a></p>';

And it works like a charm!
crazycoders
Forum Contributor
Posts: 260
Joined: Tue Oct 28, 2008 7:48 am
Location: Montreal, Qc, Canada

Re: trying to create html code within the body of a mail message

Post by crazycoders »

You are not outputting correct MIME content my friend. It will work, but the point of creating a MIME mail is not to send only HTML content. You are supposed to define a part seperator in the headers and then, each content is supposed to provide an encoding header, content type header, content-lenght header, then must be encoded and all attached together using the part seperator at beginning, end and in between parts.

second, your mime mail is a joke because it only sends HTML... The point of a MIME mail is to send text content AND html content in case the user's mail client doesn't support HTML

I suggest to simple look at the numerous excelent MIME mail libraries on the net and use them. They often come with their own SMTP server implementation which is excelent if you wish to bypass mailing limits in massmails and finaly most come with their own RFC822 implementation that allows email validation support. My package choice that is well implemented and works like a charm is htmlMimeMail5.

Have a nice day and i hope i did not bash you too badly :)
Post Reply