Sending out an email containing HTML from within a PHP page
Archive - Originally posted on "The Horse's Mouth" - 2010-11-07 19:18:44 - Graham EllisThe mail function in PHP allows you to send a plain text email in a single function call from within your script but please read the cautions at the end of this post before you do so!. How do you send out something in HTML format, though?
The mail function takes an additional (fourth) parameter of extra headers ... and one of the headers that you can use is a content-type: which lets you define the data type that's in the email - for example: text/html. There's an example showing how a message may be sent as both plain text and as HTML [here].
Email protocols are intrinsically line based, and 7 bit ... which means that what you send out needs to have a new line character at least once every 80 characters, and it cannot reliably contain high end (special) characters. In theory, that should not be a problem with HTML, but it could be a nuisance to have to split things down, and it could be more than a nuisance if you were sending out things like .jpg images. The solution here is to use a content-transfer-encoding header, with a base64 encoding - and of course to encode the data stream to suit. Full example [here].
Some Common Email Headers
The lines given here are examples of each of the most common types:
Supplied as parameters to the mail function:
To: Graham Ellis <graham@wellho.net>
Subject: A Demonstration of Email Headers
Supplied (if required) as part of the fourth (extra) parameter
From: Well House Consultants <info@wellho.net>
Reply-To: Lisa Ellis <lisa@wellho.net>
Cc: Gypsy <thedog@wellho.net>, Charlie <thecat@wellho.net>
Bcc: copybox@wellho.net, Audit <audittrail@wellho.net>
Date: Thu, 4 Nov 2010 14:30:27 +0000
Precedence: Bulk
Message-ID: 12345
In-Reply-To: wellho-5454
References: 12332, 11001, wellho-5420
MIME-Version: 1.0
A line like this sets up the document to be multipart if you wish:
Content-Type: multipart-alternative;boundary=00112233445566778899aabbccddeeff
The following can be supplied as part of the header or at the head of each section in a multipart email
Content-Type: text/html
Content-transfer-encoding: base64
Content-disposition: inline; filename="mypage.html"
The cautions at the end of this post
Before you start emailing from your server, consider the following:
a) Does the person your sending to REALLY want the email? (If it's confirming an order, the answer will be a resounding "YES", but if you're writing to tell him of your latest gizmo for sale, he might not be so keen!)
b) You're sending out an automated email from your server ... so it may look rather like other automated emails that people send out in bulk from servers (a.k.a. "spam") to many automated filters. How are you going to make your message special enough to be delivered?
c) Check that you are not providing an open emailing platform - that a user of your site is NOT able to enter a recipient's email address and message and use your server to send out unsolicited advertising material without your knowledge / approval. You really don't want to get yourself blacklisted!