Saturday, March 15, 2008

PHP Email Attachment Script

I was searching for an easy php email attachment script over net. I found a few scripts but none them able to meet my need. Most of those scripts can send one attachment. But I need to send multiple files. Then I thought to develop a script by myself. To write this function I took help from few examples that I found by searching Google.

I've checked this script with Gmail, Yahoo & Outlook. Hope it will work with other email clients.

Here is the email function which can able to send multiple attachment:

function send_email($to, $from, $from_name, $subject, $message, $attachments=false)
{
$headers = "From: ".$from_name."<".$from.">\n";
$headers .= "Reply-To: ".$from_name."<".$from.">\n";
$headers .= "Return-Path: ".$from_name."<".$from.">\n";
$headers .= "Message-ID: <".time()."-".$from.">\n";
$headers .= "X-Mailer: PHP v".phpversion();

$msg_txt="";
$email_txt = $message;

$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";

$email_txt .= $msg_txt;

$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_txt . "\n\n";


if ($attachments !== false)
{
for($i=0; $i < count($attachments); $i++)
{
if (is_file($attachments[$i]))
{
$fileatt = $attachments[$i];
$fileatt_type = "application/octet-stream";
$start= strrpos($attachments[$i], '/') == -1 ? strrpos($attachments[$i], '//') : strrpos($attachments[$i], '/')+1;
$fileatt_name = substr($attachments[$i], $start, strlen($attachments[$i]));

$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);

$data = chunk_split(base64_encode($data));

$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n";

}
}
}

$email_message .= "--{$mime_boundary}--\n";

return mail($to, $subject, $email_message, $headers);
}


Here is an example to send this script:
$to = "test@localhost.com";
$from = "admin@localhost.com";
$from_name = "Administrator";

$subject = "This is a test email";
$message = "Here is my message...Text or <b>HTML</b>.";

$attachments = array();
$attachments[]="C:/Program Files/xampp/htdocs/images/my_image.jpg"; // absolute file path here
$attachments[]="C:/Program Files/xampp/htdocs/images/my_music.mp3";

send_email($to, $from, $from_name, $subject, $message, $attachments);


Let me know your thoughts and also if you face any problem.

Bookmark It!
Subscribe in a reader

13 comments:

Anonymous said...

Thanks Zakaria, great piece of code. Ric

Anonymous said...

Hi,

Do you have any idea to count the number of file opening in the email attachment ?
i.e when ppl open my email attachment, there is a counter to it so that i may see how many it's opened.

Anonymous said...

Very good code, thanks

Anonymous said...

what about PHPMailer? I think that is best and very easy to send a mail.

Unknown said...

It says:
Parse error: syntax error, unexpected T_STRING in simplemail.php on line 1 why???

Deepak Saxena said...

You have put local path from your local pc. What if we need to browse the file from a form and send to a php variable.

Unknown said...

Nice Code, it works for me

php web development said...

I am PHP devleoper
& useful for me
Thanks for this

Anonymous said...

hi, i can't understand the absolute path. when i submit a attachment it doesn't go to email. plz help.

Zakaria Chowdhury said...

You will find absolute path by calling following function:

echo getcwd();

web development company said...

THANK YOU VERY VERY MUCH ! These tuts are just… AMAZING !

Unknown said...

Hi,
I found your post is very informative. The article is professionally written and I feel like the author knows the subject very well.Keep it that way.will we talk in this weekend through the email??


Nick
E:- hire.netprogrammer.com@gmail.com

Unknown said...

Thanks for this useful piece of code. I have one problem: the first letter of the filename is dropped. So when I send out kth_master.tsv, the file received is called th_master.tsv. Of course I can work around this by prefixing a dummy character, but that should not be necessary. I have had the same problem with another mail script, but not with a third. Any idea what might cause this?

Peter tH