Send HTML and Plain Text Versions of Email

Recently, I've been dealing with sending email from applications an awful lot. At first glance, sending a basic message from PHP is easy:

<?php mail('test@example.com', 'Subject Line', 'A message!'); ?> Sending complex messages with HTML formatting and/or attachments, however, can be tricky. Today, we're going to cover sending HTML email to a user while still including a plain text version for email clients that don't support HTML.

See the Demo | Download the Source

Part One: Understanding Email Structure

Basic email structure is something like this:

To: someone@example.com Subject: Test Email From: Test <testing@example.com> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="==PHP-alt_xd50dc5b6ee7a015fb67e36ba692a93ad961d5f7dx" X-Nonspam: None --==PHP-alt_xd50dc5b6ee7a015fb67e36ba692a93ad961d5f7dx Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit This Is a Plain Text Email This message has no HTML. http://w3schools.com --==PHP-alt_xd50dc5b6ee7a015fb67e36ba692a93ad961d5f7dx Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: 7bit <body> <h1>This Is an HTML Email</h1> <p> This message is composed in <a href="http://w3schools.com">HTML</a>. </p> </body> </html> --==PHP-alt_xd50dc5b6ee7a015fb67e36ba692a93ad961d5f7dx-- This looks a little daunting at first, but if we take... read more

ITT #16: Create a Template Parsing System with PHP

One of my most recent obsessions has been with simplifying my custom CMS to allow me to generate full sites without changing anything but HTML template files and a basic configuration file. In this week's Instant Tip Tuesday, we'll explore one of the ways we can dynamically generate output and wrap it in a template file.

NOTE: This is a proof-of-concept type of thing, not a production-ready script. Be aware that I haven't used a script like this on a real site, and therefore have no idea if it's fast, effective, or even useful. For now, this is just a "hey, cool!" sort of thing; use with discretion.

Download the Files

So, What Are We Trying to Do?

To try and explain what our goal is... read more

ITT #15: Create a Simple Contact Form

NOTE: This entry has been improved upon by adding spam protection and sessions. It's strongly recommended that you use the newer version of the simple contact form.

Making yourself accessible is one of the most important aspects of a site that aims to connect with its audience online, and nothing makes a person more accessible than an easy-to-use contact form.

In this week's Instant Tip Tuesday, we'll build a simple (less than 200 lines of code, including comments) contact form that will allow users to contact us without needing to open their mail client or copy and paste our email address into their web mail service. Our finished contact form will do the following:

  • Accept the user's name, email, URL, and a message
  • Verify that required fields were filled out
  • Validate... read more

    ITT #13: Build a Menu with Recursive Functions

    To continue my recent obsession with array handling, I want to spend this week talking about recursive functions and their application in dealing with arrays.

    The goal of our exercise today is to build a menu, complete with sub-menu, all based off one function that runs recursively.

    What Is Recursion? In computer science, recursion is a concept in which a function can call itself. This is similar to the idea of looping in PHP, but it provides an opportunity, in the case of array handling, to add more fine-grain control into a program.

    On a basic level, recursion can be illustrated with the following code:

    function plusOne($x) { if($x<10) { echo ++$x, "<br />"; plusOne($x); } else { echo 'Finished! <br />'; } } Calling this function will result in the following output:

    1 2 3 4 5 6 7 8 9 10 Finished! Obviously, the above could have been accomplished very simply with a loop... read more