Edmonds Commerce Logo
  • home
    • blog
  • ecommerce
    • product catalogue
    • order processing
    • customer services
    • stock control
    • human resources
    • management information
  • development
    • oscommerce
    • php
    • mysql
    • open source
    • performance tuning
  • design
  • marketing
  • contact us
    • pricing

Edmonds Commerce Blog

Freelance PHP Ecommerce and SEO Developer in the UK

Latest Posts

Magento Most Popular Extensions
Google Chrome for Linux Beta
Firefox Address Bar Lag + Solution
Custom Buttons for Firefox

Most Popular Posts

Magento Developer UK Freelance osCommerce UK Magento Training CRE Loaded UK

PHP Email Attachment Function

June 20th, 2008
Read More email, php

Sending an email with PHP is pretty straight forwards. It's very useful for emailing reports generated from cron jobs etc.

Sometimes though you need your application to email an attachment.

After a load of messing around I have hacked together this email attachment PHP function.

PLAIN TEXT
PHP:
  1. function email_attachment($to_email, $email, $subject,$our_email_name, $our_email, $file_location, $default_filetype='application/zip'){
  2.     $email = '<font face="arial">' . $email . '</font>';
  3.     $fileatt = $file_location;
  4.     if(function_exists(mime_content_type)){
  5.         $fileatttype = mime_content_type($file_location);
  6.     }else{
  7.         $fileatttype = $default_filetype;;
  8.     }
  9.     $fileattname =basename($file_location);
  10.     //prepare attachment
  11.     $file = fopen( $fileatt, 'rb' );
  12.     $data = fread( $file, filesize( $fileatt ) );
  13.     fclose( $file );
  14.     $data = chunk_split( base64_encode( $data ) );
  15.     //create mime boundary
  16.     $semi_rand = md5( time() );
  17.     $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
  18.     //create email  section
  19.     $message = "This is a multi-part message in MIME format.\n\n" .
  20.     "--{$mime_boundary}\n" .
  21.     "Content-type: text/html; charset=us-ascii\n" .
  22.     "Content-Transfer-Encoding: 7bit\n\n" .
  23.     $email . "\n\n";
  24.      //create attachment section
  25.     $message .= "--{$mime_boundary}\n" .
  26.      "Content-Type: {$fileatttype};\n" .
  27.      " name=\"{$fileattname}\"\n" .
  28.      "Content-Disposition: attachment;\n" .
  29.      " filename=\"{$fileattname}\"\n" .
  30.      "Content-Transfer-Encoding: base64\n\n" .
  31.      $data . "\n\n" .
  32.      "--{$mime_boundary}--\n";
  33.      //headers
  34.     $exp=explode('@', $our_email);
  35.     $domain = $exp[1];
  36.     $headers = "From: $our_email_name<$our_email>" . "\n";
  37.     $headers .= "Reply-To: $our_email"."\n";
  38.     $headers .= "Return-Path: $our_email" . "\n";    // these two to set reply address
  39.     $headers .= "Message-ID: <".time()."@" . $domain . ">"."\n";
  40.     $headers .= "X-Mailer: Edmonds Commerce Email Attachment Function"."\n";          // These two to help avoid spam-filters
  41.     $headers .= "Date: ".date("r")."\n";
  42.     $headers .= "MIME-Version: 1.0\n" .
  43.                     "Content-Type: multipart/mixed;\n" .
  44.                     " boundary=\"{$mime_boundary}\"";
  45.     
  46.     mail($to_email,$subject,$message, $headers, '-f ' . $our_email) or die ('<h3 style="color: red;">Mail Failed</h3>');
  47. }

The PHP and Email Journey Continues :-)

For a fully featured email attachment class this is definitely worth checking out. If you just want a quick function though without any bells and whistles then you should find my function will do the trick.

Possibly Relevant Posts:

  • no matches

Feed | Respond | Trackback

14 Responses to “PHP Email Attachment Function”

  1. Colleen Says:
    September 6th, 2008 at 7:36 am

    This works beautifully if 'allow_url_open' is switched on but most servers have it switched off for security reasons. I have been trying to replace the relevant bits of code with CURL but an struggling with opening the file and reading the data.

  2. Joseph Says:
    September 7th, 2008 at 5:04 pm

    If you want to attach a remote file (accessed via a URL) then I would recommend downloading the file and storing it locally before trying to send it as an attachment using this function.

  3. Markus Says:
    October 22nd, 2008 at 9:01 pm

    Excellent work.

    I've read quite a few out-dated tutorials that don't seem to work, but this one worked like a charm and was easy to understand/modify.

    Thanks!

  4. admin Says:
    October 23rd, 2008 at 8:09 am

    Thanks for the comment :-)

    if you have managed to improve the function at all it would be great if you could share it here.

  5. Edmonton Search Engine Optimization Says:
    December 9th, 2008 at 11:30 pm

    Awesome! Thank you. I have just been through every script on page one of google for "php email atatchment" and none of them worked. Your code works perfectly though. Thanks!

  6. admin Says:
    December 10th, 2008 at 8:57 am

    Glad you like :-)

  7. andre Says:
    February 28th, 2009 at 2:23 pm

    Finally someone who has a made a normal useable function.
    It seems that nobody can do this in the php community.

  8. vlad Says:
    April 21st, 2009 at 1:27 pm

    Man, I have spent so much time trying to build a function which would send a html email with sound file wav attachment. Any other tutorial I googled out did not work.

    This one worked right away!!!

    Thank you, thank you, thank you. My day is brighter. :)

  9. joseph Says:
    April 22nd, 2009 at 7:19 am

    nice one :-)

  10. Email Attachments with PHP | S-Carter.co.uk Says:
    June 19th, 2009 at 9:55 pm

    [...] of sending an email with attachment, the closest I could get to a working example I found at EdmondsCommerce.co.uk so primary credit goes to him. However I have made some minor modifications which I think make it a [...]

  11. Paul Says:
    September 30th, 2009 at 8:24 pm

    This is the best code I have found so far for this job, but still isnt working for me. I am trying to send a word doc, but its ending up as a zipped file of folders containing .xml files. Anyone know how to resolve this??

  12. admin Says:
    October 8th, 2009 at 7:16 am

    This is an issue with the new docx format

    see http://msdn.microsoft.com/en-us/library/aa338205.aspx

  13. Cron Based Database Backup - Hot Scripts Forums Says:
    October 18th, 2009 at 5:16 pm

    [...] Then, email the file with: PHP Email Attachment Function | Edmonds Commerce Blog [...]

  14. kash Says:
    January 31st, 2010 at 5:32 am

    how to create browse and attach. I have a html form that provides option to attach and send email. But I want it to browse and attach multiple files.
    How to?
    Thanks.
    Kash.

Leave a Reply

  • RSS Feed
  • Categories

    • adwords
    • apache
    • barcode
    • business
    • creloaded
    • css
    • curl
    • customer services
    • debugging
    • drupal
    • eclipse
    • ecommerce
    • edmondscommerce
    • email
    • excel
    • firefox
    • flash
    • gd
    • git
    • graphs
    • hosting
    • icecat
    • internet news
    • javascript
    • jquery
    • link building
    • linux
    • mac
    • magento
    • management
    • misc
    • mod_rewrite
    • mysql
    • netbeans
    • open suse
    • oscommerce
    • php
    • plesk
    • portfolio
    • product catalogue
    • product feed
    • programming
    • regular expressions
    • ria
    • scraping
    • search engine optimisation
    • security
    • seo
    • spidering
    • symfony
    • twitter
    • ubuntu
    • Uncategorized
    • usability
    • vps
    • web design
    • web development
    • Windows
    • xampp
    • zend framework
    • zip
  • Archives

    • February 2010
    • January 2010
    • December 2009
    • November 2009
    • October 2009
    • September 2009
    • August 2009
    • July 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008
    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
  • Tags

    bulk course cre loaded creloaded css custom developer development directories drupal error find firefox git google hosts file html jaunty javascript leeds links linux magento mysql netbeans oscommerce performance php plesk ppc problem replace search engine optimisation seo server symfony table training ubuntu uk virtualbox web web design xml zend framework
  • Random Posts

    • Twitter, Tweet Deck and 64 Bit Linux
    • Playing with Symfony
    • Google Analytics Scan
    • Yahoo + Microsoft Combined
    • Domain Names and HOSTS File
    • Magento Multistore Setup in a Nutshell
    • Magento Natively Supports Google Website Optimizer
    • Running Internet Explorer under Ubuntu Linux
    • PHP : Dead Easy Excel Export
    • Is My Site Down For Everyone or Just Me?
  • Recent Comments

    • admin on Magento Backup Error Filesystem.php on line 234 + Solution
    • admin on Magento Leeds
    • Matthew Dolley on Magento Leeds
    • kash on PHP Email Attachment Function
    • Hussein on PHP Save Images Using cURL
  • Category Specific RSS

    • adwords Feed for all posts filed under adwords
    • apache Feed for all posts filed under apache
    • barcode Feed for all posts filed under barcode
    • business Feed for all posts filed under business
    • creloaded Feed for all posts filed under creloaded
    • css Feed for all posts filed under css
    • curl Feed for all posts filed under curl
    • customer services Feed for all posts filed under customer services
    • debugging Feed for all posts filed under debugging
    • drupal Feed for all posts filed under drupal
    • eclipse Feed for all posts filed under eclipse
    • ecommerce Feed for all posts filed under ecommerce
    • edmondscommerce Feed for all posts filed under edmondscommerce
    • email Feed for all posts filed under email
    • excel Feed for all posts filed under excel
    • firefox Feed for all posts filed under firefox
    • flash Feed for all posts filed under flash
    • gd Feed for all posts filed under gd
    • git Feed for all posts filed under git
    • graphs Feed for all posts filed under graphs
    • hosting Feed for all posts filed under hosting
    • icecat Feed for all posts filed under icecat
    • internet news Feed for all posts filed under internet news
    • javascript Feed for all posts filed under javascript
    • jquery Feed for all posts filed under jquery
    • link building Feed for all posts filed under link building
    • linux Feed for all posts filed under linux
    • mac Feed for all posts filed under mac
    • magento Feed for all posts filed under magento
    • management Feed for all posts filed under management
    • misc Feed for all posts filed under misc
    • mod_rewrite Feed for all posts filed under mod_rewrite
    • mysql Feed for all posts filed under mysql
    • netbeans Feed for all posts filed under netbeans
    • open suse Feed for all posts filed under open suse
    • oscommerce Feed for all posts filed under oscommerce
    • php Feed for all posts filed under php
    • plesk Feed for all posts filed under plesk
    • portfolio Feed for all posts filed under portfolio
    • product catalogue Feed for all posts filed under product catalogue
    • product feed Feed for all posts filed under product feed
    • programming Feed for all posts filed under programming
    • regular expressions Feed for all posts filed under regular expressions
    • ria Feed for all posts filed under ria
    • scraping Feed for all posts filed under scraping
    • search engine optimisation Feed for all posts filed under search engine optimisation
    • security Feed for all posts filed under security
    • seo Feed for all posts filed under seo
    • spidering Feed for all posts filed under spidering
    • symfony Feed for all posts filed under symfony
    • twitter Feed for all posts filed under twitter
    • ubuntu Feed for all posts filed under ubuntu
    • Uncategorized Feed for all posts filed under Uncategorized
    • usability Feed for all posts filed under usability
    • vps Feed for all posts filed under vps
    • web design Feed for all posts filed under web design
    • web development Feed for all posts filed under web development
    • Windows Feed for all posts filed under Windows
    • xampp Feed for all posts filed under xampp
    • zend framework Feed for all posts filed under zend framework
    • zip Feed for all posts filed under zip

Edmonds Commerce related questions? Send us a message or call us on 0844 357 0201.

Freelance PHP Web Design UK Commercial Web Design