A simpler way to send Text or HTML emails with attachments in PHP
Author: willem In: coding, php, productivity, tips & tricks, tools, web developmentGeekology 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…”).
Related posts:
- Sending emails with attachments using PHP’s mail() function
- Validating and sanitizing URLs, Emails, and other inputs with PHP’s filter_* functions
- Using PHP and NuSOAP to connect to Web Services
- Bulk convert HTML, RTF, etc. documents to PDF using the Mac OS X command line or an AppleScript
- The Pocket HTML Tutorial
Like this post? Subscribe to the Geekology RSS 2.0 feed!













waro
December 19th, 2009 at 04:08
I’ve got a fatal error:
Fatal error: Call to undefined function get_instance() in /home/douseweb/public_html/proyek/ijeei/libs/geekmail.php on line 1718
I believe it’s an inheritance from CI. Do you have a solution to fix this error?
Constant Meiring
January 2nd, 2010 at 20:45
Kickass! Thank a heap
Constant Meiring
January 2nd, 2010 at 20:45
+s on that thank
Constant Meiring
January 3rd, 2010 at 18:23
Also, you might want to change line 368 to :
@$this->_attachType[] = $this->_mimeTypes(next(explode(’.', basename($filename))));
to shut up picky servers.
Amit Pansare
January 8th, 2010 at 02:57
For stand alone uses, instead of adapting from CodeIgniter, why not use PHPMailer - http://phpmailer.worxware.com/index.php?pg=phpmailer?
Not advocating it, just trying to reason.
Amit Pansare
January 8th, 2010 at 02:59
Correction in above URL : http://phpmailer.worxware.com/
Mart Buchwald
January 27th, 2010 at 17:15
This is really good, just got it working.
How can I use this to check if the email has bounced?
thanks
willem
January 28th, 2010 at 02:44
Hey Mart
You won’t be able to use this class to check for bounces, but you could set up a PHP script to connect to your email server via POP or IMAP, read new messages, and take note of ones that contain the text “MAILER-DAEMON”?
willem
January 28th, 2010 at 17:43
@waro: Thanks for letting me know, I’ve now corrected the issue and the correct version of the script is available for download.
semicton
February 2nd, 2010 at 18:52
Cool thanks so much for this. I had to change the code a bit to accept temp files uploaded to the server and then be attached but it works perfectly. All the other email attachments I’ve tried were not working in apple mail.