1

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!

  • 1
    Set the Content-Type to `multipart/alternative` as in http://stackoverflow.com/questions/4897215/php-mail-how-to-send-html - Or see the many *Related* links on sending html mails, which conclusively recommend not to do this manually with the bare `mail()` function. – mario May 12 '11 at 19:43

2 Answers2

2

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.

onteria_
  • 68,181
  • 7
  • 71
  • 64
  • I think this is the best advertisement *ever* for SwiftMailer -- compare the horrible process of assembling your own MIME body to the simple awesomeness of the SwiftMailer API. – Charles May 12 '11 at 19:45
1

Everything is here (Example #4): http://php.net/manual/en/function.mail.php

zzarbi
  • 1,832
  • 3
  • 15
  • 29