HomePHP Page 4 - Composing Messages in HTML for MIME Email with PHP
Sending email in HTML format using the MIME mailer class - PHP
Welcome to the fourth episode of the series, "Sending MIME email with PHP." In this article, I’m going to show you how to provide the MIME mailer class with the ability to send email messages in HTML format. This will greatly extend its functionality, so don't miss this tutorial.
Below I coded a basic script, which comes in handy for illustrating the functionality of the “Mailer” class, especially when it comes to sending email messages in HTML.
Here’s the corresponding code sample:
// create a new instance of the 'Mailer' class
$mailer=&new Mailer('alejandro@mydomain.com,'mybuddy@yourdomain.com','Testing mailer class','Hello buddy. I am sending to you a couple of fun images.');
$mailer->addHTML('<p>This is a simple paragraph formatted in HTML</p>');
$mailer->addHTML('<p>This is another paragraph formatted in HTML</p>');
// add some attachments
$mailer->addAttachment('file1.gif');
$mailer->addAttachment('file2.gif');
// send MIME email
$mailer->send();
As shown in the above example, I used the MIME mailer class to send a simple text message with a couple of attached files. However, I’m also sending HTML code embedded into the message in question.
As usual, I suggest you to test the previous “Mailer” class by incorporating your own improvements. It’s going to be an educational experience, trust me!
Final thoughts
That’s all for now. In this fourth installment of the series, I taught you how to build a modular MIME mailer class that’s capable of sending email in both plain text and HTML formats. In addition, the class supports working with file attachments, so I guess it should be quite helpful when it comes to learning the basics of sending MIME email with PHP.
In the last chapter, I’ll be porting the complete source code of the class to PHP 5. This will take advantage of its improved object model. Don’t miss the upcoming article!