954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Sending html emails

Hello, I've been trying to send html emails via the mail() function. I tried different combinations of headers configurations. and spent hours and hours on this with no luck. All I get is plain text, as if i did not define the headers. headers would be displayed in the message as well.

I am pretty sure it is not a matter of correct headers. as i literally tried everything.

Just can figure out what is preventing it from encoding the message as html.

appreciate your help!

thanks.

roy--
Light Poster
25 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

Probably the easiest way to go would be to use the mailer from the PEAR package for php, all you do is set isHTML to true and it will write the headers and all that good stuff for you. It makes mail 100 times easier.
Here's the link for you
http://pear.php.net/package/Mail

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

Thanks shawn,

I managed to get PEAR working on my box. and am sending emails. but I still have the same problem of emails sent without html encoding.

i think its something to do with the settings of Mail. cant find any documentation on html encoding on the PEAR Mail website.

this is my php script:

require 'Mail.php';
$recipients = 'roy@example.com';

$headers['From']    = 'contact@example.net';
$headers['To']      = 'roy@example.com';
$headers['Subject'] = 'Test message';

$body = 'Test <b>message</b>';

//$params['sendmail_path'] = '/usr/sbin/sendmail -t -i';
$params['sendmail_path'] = '/usr/sbin/sendmail';
$params['isHTML'] = 'true';

// Create the mail object using the Mail::factory method
$mail_object =& Mail::factory('sendmail', $params);
$mail_object->send($recipients, $headers, $body) or die("could not send email");


and this is the email output:

Test<b>message</b>

thanks

roy--
Light Poster
25 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

This is using the PEAR mailer package

require("phpmailer/class.phpmailer.php");

// personal info ommitted
$mail = new PHPMailer();
    $mail->From = "Application@NarutoFor.Us";
    $mail->AddAddress($email);
    $mail->FromName = "NarutoMUD";    
    $mail->Subject = "Thank you for applying to NarutoMUD";
    $replymsg = str_replace("%RESPONSE%", "{$response}", $replymsg);
    $mail->Body = $replymsg;
    $mail->WordWrap = 50;
    $mail->AltBody = $altmsg;
    $mail->IsHTML(true); //note this line
    if(!$mail->Send())
    {
       echo 'Message was not sent.';
       echo 'Mailer error: ' . $mail->ErrorInfo;
    }
ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

add mimetypes to your code...

fatihpiristine
Posting Whiz in Training
283 posts since Sep 2007
Reputation Points: 6
Solved Threads: 19
 
add mimetypes to your code...


the PEAR mailer class automatically adds the text/html mimetype to the headers when you say implementation->isHTML = true;

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

but the result is still plain text.

fatihpiristine
Posting Whiz in Training
283 posts since Sep 2007
Reputation Points: 6
Solved Threads: 19
 

I'm not sure what implementation you are using because, obviously, it looks very different from the one I'm using. There wasn't really any "installing" when using the PEAR mailer. It was just downloading the class.phpmailer.php file and using the functions provided.

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You