I wasted a lot of time trawling the internet for an example 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 little bit easier/nicer.
Copy this straight into a new text file on your web server and modify the first line starting ‘email_attachment( ‘ to represent your own details. Open it up in your browser, and then check your email inbox.
<?php
email_attachment('to.this@address.com', 'my.email@address.com', 'Email attachment', 'My Name', 'my.email@address.com', 'myfile.jpg');
function email_attachment($to_email, $email, $subject, $our_email_name, $our_email, $file_location, $default_filetype='application/zip'){
$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 .= "Date: ".date("r")."\n";
$headers .= "MIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
if (mail($to_email,$subject,$message, $headers, '-f ' . $our_email))
echo 'Success';
else
echo 'Error';
}
?>
Hi there,
blog.s-carter.co.uk to GoogleReader!
Thanks
Robor
Hi Robor,
You can access the site in google reader already as you’ve specified
Thanks for providing me best deatails for sending mail.