In PHP, how can I send an HTML email with an attachment?
I have a form that sends to myself and allows people to attach a file. But I can't seem to allow HTML in the message. I think I need two different Content-Type:
$to = "my@email.com"; // Webmaster
$subj = "Great Subject";
$message = strip_tags($_POST['message']);
$attachment = chunk_split(base64_encode(file_get_contents($_FILES['attachment']['tmp_name'])));
$filename = $_FILES['attachment']['name'];
$boundary =md5(date('r', time()));
$headers = "From: $fname $lname <$email>开发者_开发知识库\r\nReply-To: $fname $lname <$email>";
$headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\"";
$message .= "Here is your <strong>File</strong>.".\r\n" ;
$message .= "This is a multi-part message in MIME format.
--_1_$boundary
Content-Type: multipart/alternative; boundary=\"_2_$boundary\"
--_2_$boundary
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit
$message
--_2_$boundary--
--_1_$boundary
Content-Type: application/octet-stream; name=\"$filename\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--_1_$boundary--";
mail($to, $subj, $message, $headers);
Use a class such as PHPMailer, as it will make it simpler.
精彩评论