Monday, February 7, 2011

Sending Email With Multiple Attachments in PHP

This script is really helpful for those who want to send email with multiple attachments in PHP. There are scripts out there but most of them have some errors in them but i assure you that this script is error free.


If you guys face any error please let me know and i will try my best to assist you.


<?php
function SendAttachMail($to,$from,$sub,$msg,$i)
{
$flag_attach = true;
  // Who the email is from
$email_from = $from;
// The Subject of the email  
$email_subject = $sub;
// Message that the email has in it 
$email_txt = $msg;
// Add the headers for a file attachment 
$headers = "From: $email_from";

// Create a boundary string. It must be unique 
// So we use the MD5 algorithm to generate a random hash
$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_message .= "This is a multi-partmessage 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";


$fileatt_type = "application/octet-stream";
$fileatt = $_FILES['fileatt'.$i]['tmp_name'];
$fileatt_type = $_FILES['fileatt'.$i]['type'];
$fileatt_name = $_FILES['fileatt'.$i]['name'];

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


 //Read the contents of atachment file into a string and then encode it 
 with MIME base64 and split it into smaller chunks
$data = chunk_split(base64_encode($data));

 //Define the body of the email message.
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";

$email_message = $email_message.$email_txt;
@mail($to, $email_subject, $email_message, $headers);
}


if(isset($_POST['cmdsend']))

 // Now i will write the code to show how the attachments will be 
 passed to the  SendAttachMail() function.

 // The email to whom the message will be send
$to="to@gmail.com";

// Who the email is from
$from="from@gmail.com";

// Subject of the email
$sub=$_POST['em_sub'];

// Email body message
$msg=$_POST['em_msg'];

// Code for multiple attachments. I am doiing this for 3 
 attachments but you can attach as many you want just increasing 
 the number in the code

for($i=1; $i<=3; $i++)
{
$file_name=$_FILES['fileatt'.$i]['name'];

if(trim($file_name)!='')
{
SendAttachMail($to,$from,$sub,$msg,$i);
}
}
}
?>


The html part for this code is:
<!-- Forms action is set to the same page -->
<form name="frmemail" enctype="multipart/form-data" method="post" action="">
Enter Subject: <input type="textbox" name="em_sub"><br>
Enter Email Message: <input type="textbox" name="em_msg"><br>
Attach File1: <input type="file" name="fileatt1"><br>
Attach File2: <input type="file" name="fileatt2"><br>
Attach File3: <input type="file" name="fileatt3"><br>
<input type="submit" name="cmdsend" value="Send Email">
</form>

No comments:

Post a Comment