开发者

In PHP, how can I send an HTML email with an attachment?

开发者 https://www.devze.com 2023-03-20 19:12 出处:网络
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:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消