Geekology previously posted an article on sending emails with attachments using PHP’s mail() function.

However, some readers experienced problems with getting the linebreaks and spacing exactly right on certain platforms (sending multipart emails using the mail() function in PHP is unfortunately a very precise exercise).

This post details a simpler method of creating and sending Text / HTML email messages with attachments in PHP using an adapted version of the Email Library that is included in the CodeIgniter PHP Framework.

Using the geekMail PHP class:

First, you’ll need to download the latest version of the geekMail PHP class here.

After including the class file in your project, use a PHP script like the following to build and send your multipart email messages:

require 'geekMail-1.0.php';
 
$geekMail = new geekMail();
 
$geekMail->setMailType('html');
 
$geekMail->from('noreply@geekology.co.za', 'Geekology');
 
$geekMail->to('willem@geekology.co.za');
//$geekMail->cc('willem@geekology.co.za');
//$geekMail->bcc('willem@geekology.co.za');
 
$geekMail->subject('Example subject');
 
$geekMail->message('This is an <strong>example</strong> message.');
 
$geekMail->attach('/home/willem/file1.txt');
$geekMail->attach('/home/willem/file2.zip');
 
if (!$geekMail->send())
{
  $errors = $geekMail->getDebugger();
  print_r($errors);
}

Since the geekMail class is an adaptation of the Email Library used in CodeIgniter, most of the official CodeIgniter documentation for the library should work. One exception of course is that the library won’t be dynamically loaded from a CodeIgniter controller (“$this->load->…” and “$this->email->from…”).

Share this article: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Twitter
  • GatorPeeps
  • Digg
  • Reddit
  • muti.co.za
  • DZone
  • del.icio.us
  • StumbleUpon
  • Technorati
  • Ma.gnolia
  • Slashdot

Related posts:

  1. Sending emails with attachments using PHP’s mail() function
  2. Validating and sanitizing URLs, Emails, and other inputs with PHP’s filter_* functions
  3. Using PHP and NuSOAP to connect to Web Services
  4. Bulk convert HTML, RTF, etc. documents to PDF using the Mac OS X command line or an AppleScript
  5. The Pocket HTML Tutorial