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 Post Your Comment View Comments (13)

Thursday, March 6, 2008

Free Tools for Web Developers

Today I have found a nice article in a blog name Jatecblog about free tools for web development. The article name is - Links for Making Your First Web Site Without Paying a Dime. In this article the author listed all necessary free tutorial website links, Graphics Softwares, Templates, Text Editors, WYSIWYG creators, Testing Sites, Free Hosting sites, Free Domain links & FTP clients software links.

I think you will find that article very helpful & you may want to bookmark it for future reference.

Bookmark It!
Subscribe in a reader Post Your Comment View Comments (1)