I want to send emails as html.
Butfunction send emails as plain text.PHP Code:
Can I send email as html with "mail" function?
Printable View
I want to send emails as html.
Butfunction send emails as plain text.PHP Code:
Can I send email as html with "mail" function?
Yes you can. You need you need to send the email as an MIME (Muiltipart Internet Mail Extensions) type. The HTML portion of the email is actually an inline attachment.
This tutorial shows how it is done:
http://www.sitepoint.com/article/advanced-email-php/3
Here's a code i found on PHP.net once ... it helped me alot ... you even can send attachments via it :-)
Enjoy !Code:<?php
$boundary = '-----=' . md5( uniqid ( rand() ) );
?>
You can attach a Word document if you specify:
<?php
$message .= "Content-Type: application/msword; name=\"my attachment\"\n";
$message .= "Content-Transfer-Encoding: base64\n";
$message .= "Content-Disposition: attachment; filename=\"$theFile\"\n\n";
?>
When adding a file you must open it and read it with fopen and add the content to the message:
<?php
$path = "whatever the path to the file is";
$fp = fopen($path, 'r');
do //we loop until there is no data left
{
$data = fread($fp, 8192);
if (strlen($data) == 0) break;
$content .= $data;
} while (true);
$content_encode = chunk_split(base64_encode($content));
$message .= $content_encode . "\n";
$message .= "--" . $boundary . "\n";
?>
Add the needed headers and send!
<?php
$headers = "From: \"Me\"<[email protected]>\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"";
mail('[email protected]', 'Email with attachment from PHP', $message, $headers);
?>
Finally, if you add an image and want it displayed in your email, change the Content-Type from attachment to inline:
<?php
$message .= "Content-Disposition: inline; filename=\"$theFile\"\n\n";
?>