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.

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

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

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;
    }
Member Avatar for fatihpiristine

add mimetypes to your code...

add mimetypes to your code...

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

Member Avatar for fatihpiristine

but the result is still plain text.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.