home | contact us
» » June


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.

function email_attachment($to_email, $email, $subject,$our_email_name, $our_email, $file_location, $default_filetype='application/zip'){
	$email = '<font face="arial">' . $email . '</font>';
	$fileatt = $file_location;
	if(function_exists(mime_content_type)){
		$fileatttype = mime_content_type($file_location); 
	}else{
		$fileatttype = $default_filetype;;
	}
	$fileattname =basename($file_location);
	//prepare attachment
	$file = fopen( $fileatt, 'rb' ); 
	$data = fread( $file, filesize( $fileatt ) ); 
	fclose( $file );
	$data = chunk_split( base64_encode( $data ) );
	//create mime boundary
	$semi_rand = md5( time() ); 
	$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
	//create email  section
	$message = "This is a multi-part message in MIME format.\n\n" . 
	"--{$mime_boundary}\n" . 
	"Content-type: text/html; charset=us-ascii\n" . 
	"Content-Transfer-Encoding: 7bit\n\n" . 
	$email . "\n\n";
	 //create attachment section
	$message .= "--{$mime_boundary}\n" . 
	 "Content-Type: {$fileatttype};\n" . 
	 " name=\"{$fileattname}\"\n" . 
	 "Content-Disposition: attachment;\n" . 
	 " filename=\"{$fileattname}\"\n" . 
	 "Content-Transfer-Encoding: base64\n\n" . 
	 $data . "\n\n" . 
	 "--{$mime_boundary}--\n";
	 //headers
	$exp=explode('@', $our_email);
	$domain = $exp[1];
	$headers = "From: $our_email_name<$our_email>" . "\n";
	$headers .= "Reply-To: $our_email"."\n";
	$headers .= "Return-Path: $our_email" . "\n";    // these two to set reply address
	$headers .= "Message-ID: <".time()."@" . $domain . ">"."\n";
	$headers .= "X-Mailer: Edmonds Commerce Email Attachment Function"."\n";          // These two to help avoid spam-filters
	$headers .= "Date: ".date("r")."\n";
	$headers .= "MIME-Version: 1.0\n" . 
                    "Content-Type: multipart/mixed;\n" . 
                    " boundary=\"{$mime_boundary}\"";
	 
	mail($to_email,$subject,$message, $headers, '-f ' . $our_email) or die ('<h3 style="color: red;">Mail Failed</h3>');
}

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.


 

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.

function email_name($email_address, $split='@'){
	return ucwords(strtolower(substr($email_address, 0, strripos($email_address, $split))));
}

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.


 

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.

Furthermore, Edmonds Commerce also specialise in all osCommerce Derived Systems including CRE Loaded, Zen Cart etc.


 

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.

 function sleep_flush($chunks=10){

 	$c=0;

 	while($c < $chunks){

 		$rand = rand(2000000, 6000000);

 		echo '<br> . . . sleeping for ' . round(($rand / 1000000),2) . ' seconds . . . zzzzzzzzzzzzzz<br>';

 		flush();

 		usleep($rand);

 		$c++;

 	}

}

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.


 
rss icon