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

Is My Site Down For Everyone or Just Me?
Magento Natively Supports Google Website Optimizer
PHPNW08 - PHP Conference Up North!!
Javascript Debugging

Most Popular Posts

PHP Email Attachment Function Freelance osCommerce UK Ultimate osCommerce Checkout - Fast and Friendly PHP : Dead Easy Excel Export

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.

Bookmark this Post
Add 'PHP Email Attachment Function' to Del.icio.usAdd 'PHP Email Attachment Function' to diggAdd 'PHP Email Attachment Function' to FURLAdd 'PHP Email Attachment Function' to blinklistAdd 'PHP Email Attachment Function' to redditAdd 'PHP Email Attachment Function' to Feed Me LinksAdd 'PHP Email Attachment Function' to TechnoratiAdd 'PHP Email Attachment Function' to Yahoo My WebAdd 'PHP Email Attachment Function' to NewsvineAdd 'PHP Email Attachment Function' to SocializerAdd 'PHP Email Attachment Function' to Ma.gnoliaAdd 'PHP Email Attachment Function' to Stumble UponAdd 'PHP Email Attachment Function' to Google BookmarksAdd 'PHP Email Attachment Function' to RawSugarAdd 'PHP Email Attachment Function' to SquidooAdd 'PHP Email Attachment Function' to SpurlAdd 'PHP Email Attachment Function' to BlinkBitsAdd 'PHP Email Attachment Function' to NetvouzAdd 'PHP Email Attachment Function' to RojoAdd 'PHP Email Attachment Function' to BlogmarksAdd 'PHP Email Attachment Function' to ShadowsAdd 'PHP Email Attachment Function' to Co.mments
Add 'PHP Email Attachment Function' to ScuttleAdd 'PHP Email Attachment Function' to BloglinesAdd 'PHP Email Attachment Function' to TailrankAdd 'PHP Email Attachment Function' to SegnaloAdd 'PHP Email Attachment Function' to OKnotizieAdd 'PHP Email Attachment Function' to NetscapeAdd 'PHP Email Attachment Function' to Bookmark.itAdd 'PHP Email Attachment Function' to AskAdd 'PHP Email Attachment Function' to SmarkingAdd 'PHP Email Attachment Function' to LinkagogoAdd 'PHP Email Attachment Function' to DeliriousAdd 'PHP Email Attachment Function' to SocialdustAdd 'PHP Email Attachment Function' to Live-MSNAdd 'PHP Email Attachment Function' to SlashDotAdd 'PHP Email Attachment Function' to SphinnAdd 'PHP Email Attachment Function' to DiggitaAdd 'PHP Email Attachment Function' to SeotribuAdd 'PHP Email Attachment Function' to FaceBookAdd 'PHP Email Attachment Function' to UpnewsAdd 'PHP Email Attachment Function' to WikioAdd 'PHP Email Attachment Function' to Social Bookmarking Reloaded

Feed | Respond | Trackback

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

Leave a Reply

  • RSS Feed
  • Categories

    • apache
    • barcode
    • business
    • creloaded
    • curl
    • customer services
    • debugging
    • ecommerce
    • email
    • excel
    • firefox
    • flash
    • gd
    • graphs
    • hosting
    • icecat
    • internet news
    • javascript
    • link building
    • linux
    • magento
    • management
    • mod_rewrite
    • mysql
    • oscommerce
    • php
    • plesk
    • product catalogue
    • product feed
    • programming
    • regular expressions
    • scraping
    • search engine optimisation
    • security
    • seo
    • spidering
    • ubuntu
    • vps
    • web design
    • web development
    • Windows
    • xampp
    • zip
  • Archives

    • November 2008
    • October 2008
    • September 2008
    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
  • Tags

    addons advanced adverts blackhat blocking css development directories find firefox google hosts file html javascript keywords links msn mysql myths operators oscommerce paid links paid placement performance php ppc reciprocal linking replace screen scraping search engine optimisation security seo serp speed spider spidering tuning user friendly vista web web design web developer
  • Random Posts

    • Using cURL with XAMPP
    • HOSTS file, Vista and Blocking Unwanted Adverts
    • HP Printer on Ubuntu
    • osCommerce Password Reset Using phpMyAdmin
    • MySQL Dump Partial Restore - Split MySQL Dump into Tables
    • Populating osCommerce and other Ecommerce Platforms
    • Is My Site Down For Everyone or Just Me?
    • PHP Email Attachment Function
    • File Comparison on Ubuntu
    • Apache, mod_rewrite and SEO

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

Freelance PHP Web Design UK Commercial Web Design