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

CRELoaded Remove Google Ads -
ICECat Integration with osCommerce, Magento etc
Magento UK
PHP Cached Download Function

Most Popular Posts

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

Archive for June, 2008

PHP Email Attachment Function

Friday, June 20th, 2008

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.

Posted in email, php | No Comments »

Get Name from Email Address

Wednesday, June 11th, 2008

Check out this simple and easy function for extracting the name from an email address. It's not very advanced but it is quite handy. Of course it could be used for similar applications by simply changing the character to split on.

PLAIN TEXT
PHP:
  1. function email_name($email_address, $split='@'){
  2.     return ucwords(strtolower(substr($email_address, 0, strripos($email_address, $split))));
  3. }

There are some very powerful things you can do with PHP and email. Check out this POP3 email class for some ideas. I am just starting to scratch the surface of this functionality and will doubtless have some more great things to show soon.

Posted in php | No Comments »

Freelance osCommerce UK

Monday, June 9th, 2008

It seems that there are many UK webmasters who struggle to find a reliable UK based web developer who specialises in PHP. I have heard stories of people who claim to be osCommerce experts but in reality they simply know how to install a few contributions and that's about it.

If you are looking for a freelance osCommerce developer, especially if you would prefer to deal with someone else in the United Kingdom then you would do well to get in touch with me. I now have a long list of happy clients who have had their osCommerce sites improved, speeded up and automatically populated with huge catalogues.

So to clarify. Edmonds Commerce is a Freelance web developer who specialise in osCommerce and is UK Based.

Posted in php | No Comments »

PHP Random Sleep Function with Flush

Monday, June 9th, 2008

Sometimes you want your script to pause for a short period of time before repeating a loop or proceeding to the next step. This may be to reduce server load or even to simulate the natural pauses that a person would make whilst browsing a site. This kind of thing is especially true if you are building a system to scrape content such as product information from a suppliers web site and you do not want to hammer their server to death and get your IP banned.

PLAIN TEXT
PHP:
  1. function sleep_flush($chunks=10){
  2.  
  3.     $c=0;
  4.  
  5.     while($c <$chunks){
  6.  
  7.        $rand = rand(2000000, 6000000);
  8.  
  9.        echo '<br> . . . sleeping for ' . round(($rand / 1000000),2) . ' seconds . . . zzzzzzzzzzzzzz<br>';
  10.  
  11.        flush();
  12.  
  13.        usleep($rand);
  14.  
  15.        $c++;
  16.  
  17.     }
  18.  
  19. }

This function will do just that for you. Also it has a built in flush which will help in preventing the script from being regarded as timed out if you are running it from the web browser.

Posted in php, scraping | No Comments »

  • RSS Feed
  • Categories

    • apache
    • barcode
    • 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
    • spidering
    • ubuntu
    • web design
    • web development
    • Windows
    • xampp
    • zip
  • Archives

    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
  • Tags

    addons advanced adverts blackhat blocking css curl 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 security seo serp speed spider spidering tuning user friendly vista web web design web developer
  • Random Posts

    • Faster File Browsing with Tabs for Ubuntu
    • Apache, mod_rewrite and SEO
    • PHP Email Attachment Function
    • Building Spiders: Grab Data, Post Forms and Interact with Web Sites Automatically
    • Populating osCommerce and other Ecommerce Platforms
    • PHP Random Sleep Function with Flush
    • HOSTS file, Vista and Blocking Unwanted Adverts
    • Advanced Google Search Queries
    • MySQL Dump Partial Restore - Split MySQL Dump into Tables
    • osCommerce Contribution Released: Server Migration Synchronisation

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

Freelance PHP Web Design UK Commercial Web Design