A simpler way to send Text or HTML emails with attachments in PHP

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-&gt;message('This is an <strong>example</strong> message.');
 
$geekMail-&gt;attach('/home/willem/file1.txt');
$geekMail-&gt;attach('/home/willem/file2.zip');
 
if (!$geekMail-&gt;send())
{
  $errors = $geekMail-&gt;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:

  1. Sending emails with attachments using PHP’s mail() function
  2. Forcing a Return-Path header when sending email with PHP’s mail() function
  3. Testing SMTP servers from a UNIX command line
  4. Using PHP and NuSOAP to connect to Web Services
  5. Validating and sanitizing URLs, Emails, and other inputs with PHP’s filter_* functions
Twitter Digg Delicious Stumbleupon Technorati Facebook Email

21 Responses to “A simpler way to send Text or HTML emails with attachments in PHP”

  1. 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?

  2. Also, you might want to change line 368 to :
    @$this->_attachType[] = $this->_mimeTypes(next(explode(‘.’, basename($filename))));
    to shut up picky servers.

  3. 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.

  4. Mart Buchwald Reply 27 Jan 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

  5. 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”?

  6. @waro: Thanks for letting me know, I’ve now corrected the issue and the correct version of the script is available for download.

  7. 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.

  8. Mart Buchwald Reply 24 Feb 2010 at 17:59

    email_send_failure_phpmail

    From: “Blaxx Ltd”
    Return-Path:
    Reply-To: “abdul@blaxx.co.uk”
    X-Sender: abdul@blaxx.co.uk
    X-Mailer: Getsmarter
    X-Priority: 3 (Normal)
    Message-ID:
    Mime-Version: 1.0
    Content-Type: multipart/mixed; boundary=”B_ATC_4b854c0ae911e”

    This is a multi-part message in MIME format.
    Your email application may not support this format.

  9. Hi,
    How would you insert and inline image somewhere in the email body of the html version ?

    Thanks

  10. I’ve been testing the two versions, this one and the other one under Related Posts, all day and can’t for love nor money get my message body to show. The only time it does is when I attach a text file – then the message body is a copy of what’s in my text file.
    I’ve done everything that anyone suggests trying but no message body at all comes up. I can see it in the message source though.
    Any further ideas on what I’m missing or doing wrong? Tx.

  11. This is great, it works a dream. How can I add in the reply-to option?
    Many thanks,
    John

  12. Excellent code

  13. Hi. Thanks for the script. I’ve been having a lot of problems getting the email to send with both a valid file (bigger than 0kb) AND having the message display correctly when using other scripts that are out there. Got almost immediate success with this file, so thank you very much!

    One thing I will mention is that I had to convert all instances of ‘>’ to ‘>’ (without quotes) in order for the script to run properly.

    Once again, thank you very much. This has ended a few days worth of searching!

    Nice site layout, too. Very clean :)

  14. Hi. I just notice that my previous comment now looks ridiculous as it reads:

    I had to convert all instances of ‘>’ to ‘>’

    The first ‘>’ is actually supposed to be escaped. I could try and write it as ‘& gt ;’ (without quotes or spaces). This is what i meant to display, but of course it is being rendered as a > in my first comment. Sorry!

  15. Thanx a lot maite…….
    Works well…..

  16. Wow! That was way too easy. With the rest of the thankful family, i’m in!
    God bless

  17. Thanks for your excellent work on this…

Afrigator