HomePHP Page 4 - Handling Attachments in MIME Email with PHP
Seeing the Mailer class in action - PHP
In this second tutorial of the series, I’m going to teach you how to improve the initial structure of the MIME mailer class developed in the first article by giving it the ability to work directly with attachments. Sounds fairly interesting, right? Then don’t waste any more time in preliminaries and begin reading this article now!
I listed the complete definition of this improved MIME “Mailer” class below along with a short script that demonstrates how to use it to send a trivial message that incorporates a pair of attached files.
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. Is everything doing fine over there?');
// add some attachments
$mailer->addAttachment('file1.gif');
$mailer->addAttachment('file2.gif');
// send MIME email
$mailer->send();
That’s all the source code that you need to get the “Mailer” class working as expected. For this particular case, I utilized the class to send a simple message in plain text along with a couple of attached GIF files. This should clearly show its considerable functionality.
As always, you’re free to use the code samples shown in this tutorial in case you want to use the previous “Mailer” class to improve your existing skills in sending MIME email with PHP.
Final thoughts
That’s all for the moment. In this second chapter of the series, I taught you how to extend the existing functionality of the previous MIME mailer class in order to provide it with the capacity to work with attachments.
In the next tutorial, I’m going to improve the business logic implemented by the class, particularly when it comes to determining the MIME type of an attached file. This will make it slightly more compact and efficient.
Now that you’ve been warned about the subject of the upcoming article, you won’t want to miss it!