Sending Nice HTML Email with PHP | CSS-Tricks – CSS-Tricks

This will be a continuation of the website change request form demo that we’ve been using here for a while. if you need to catch up, I talked about it first, then built it, then screencast it, then secured it. Throughout all of this, the end result has been a boring text-only email sent to a single email address.

let’s improve that result and convert the email into a nicer html formatted email.

not much different than text email

sending html email via php uses exactly the same email function as text email:

The last parameter, the headers, are optional to the function but required to send html emails, as this is where we can pass the content-type declaration that tells email clients to parse the email as html.

In fact, the header area gives us the opportunity to perform many important email functions. this is where we can set the settings for: and reply to: if needed, as well as cc and bcc from other recipients (hey, a checkbox to send to yourself would be a cool feature to add!). here is the code used for the new and improved html-sendin website change request form:

now we can use html tags

the message parameter (a big string that we pass to the mail function with the body of our email), can now have html tags. for example:

See Also:  Here's how to strikethrough text in gmail

here’s the whole thing, to create the datatable as shown above:

I think it looks much better. and since this email is (theoretically) coming directly to you, isn’t it nice to know that it will be formatted to be easy on the eyes?

concerns

some people just absolutely hate html email. on the one hand, it can be a security risk, since it is possible to run javascript on them in some email clients, which can be problematic. html emails also have a habit of being more easily caught in spam filters. I think it’s less of a concern here, as this email is essentially being created by you for you.

demo

I updated the demo and downloaded it to use the new html email format.

view demo download files

there are likely to be one or two more demos on this form to come. I would at least like to make one to write the data to a database before sending the email. email can be such a fragile thing that saving the data to a database first is definitely a smart move.

Leave a Reply

Your email address will not be published. Required fields are marked *