Copyright © 2008 by Daniel G. Delaney, Fluid Mind Software
This script is free and open source. You may copy and modify it in any
way you wish as long as you keep the copyright information at the top of the script.
This class provides standard functionality for creating and sending an email message using the UTF-8 encoding for the message body and all headers. It can handle plain-text and HTML bodies (or both), and file attachments. You can instantiate it and send a message manually, or you can make a separate class that extends this class for each email message your system needs to send.
Download the PHP script as a zipped file (you might have to right-click that link and choose "Save as"). Unzip it and place it in the directory where you've set PHP to automatically look for your classes (you should always use the __autoload() function to set that).
Plain text message:
$email = new EmailHandler(); $email->setFrom('joe@example.com', 'Joe Schmoe'); $email->setSubject('This is a test'); $email->setBody('This is a test'); $email->addRecipient('jane@example.com', 'Jane Schmoe'); $email->send();
HTML message with an attachment:
$email = new EmailHandler(); $email->setFrom('joe@example.com', 'Joe Schmoe'); $email->setSubject('This is a test'); $email->setHtmlBody('<html><head><title>This is a test</title></head>' . '<body><p>This is a test</p></body></html>'); $email->addRecipient('jane@example.com', 'Jane Schmoe'); $email->addAttachment('/path/to/some/file.txt'); $email->send();