简体   繁体   中英

PHP mail() specific question

So, I would like to use mail() to send registration emails for my website, however I'd like to make it look nice while falling back to good old plaintext when necessary; a mixed message email.

However I would like it to be sent from John Doe who's email is johndoe@example.com to recipient@example.com.

The HTML code should be <html><head><title>HTML email!</title></head><body><p>This is HTML!</p></body</html> and the plaintext message should be This is plaintext.

What would be the arguments to mail() to accomplish this? I know a lot of it deals with changing the header in some crazy way.

Thanks so much!

Use something like SwiftMailer instead, as it has nice things like header injection prevention. With that in mind, yes, you have to set custom headers and use a multi-part body to achieve what you want:

/***************************************************************
 Creating Email: Headers, BODY
 1- HTML Email WIthout Attachment!! <<-------- H T M L ---------
 ***************************************************************/
#---->Headers Part
$Headers     =<<<AKAM
From: $FromName <$FromEmail>
Reply-To: $FromEmail
MIME-Version: 1.0
Content-Type: multipart/alternative;
    boundary="$boundary1"
AKAM;

#---->BODY Part
$Body        =<<<AKAM
MIME-Version: 1.0
Content-Type: multipart/alternative;
    boundary="$boundary1"

This is a multi-part message in MIME format.

--$boundary1
Content-Type: text/plain;
    charset="windows-1256"
Content-Transfer-Encoding: quoted-printable

$TextMessage
--$boundary1
Content-Type: text/html;
    charset="windows-1256"
Content-Transfer-Encoding: quoted-printable

$HTMLMessage

--$boundary1--
AKAM;

Source: http://www.php.net/manual/en/function.mail.php#83491

This is a lot of work. Which, once again, is why I recommend having a library that can handle all of this for you, plus other features.

一切都在这里(示例4): http : //php.net/manual/en/function.mail.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM