Jump to content

Send Multiple Attachments


harchew

Recommended Posts

Basically I have successfully found a function that can send email with attachment. However wasn't able to send multiple attachments. I tried sending two attachments manually, but only the second was successfully attached. Can someone look through the code below and point out my mistake. Thank you in advance....

 

 

<?php

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

{

$fileatt_one = $_FILES['attachment']['tmp_name'][0];

$fileatt_type_one = "application/octet-stream"; // File Type

$fileatt_name_one = $_FILES['attachment']['name'][0]; // Filename that will be used for the file as the attachment

 

$fileatt_two = $_FILES['attachment']['tmp_name'][1];

$fileatt_type_two = "application/octet-stream"; // File Type

$fileatt_name_two = $_FILES['attachment']['name'][1]; // Filename that will be used for the file as the attachment

 

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

$email_subject = "Test"; // The Subject of the email

$email_txt = "Testing 123"; // Message that the email has in it

$email_to = "a@gmail.com"; // Who the email is too

 

$headers = "From: ".$email_from;

 

$file_one = fopen($fileatt_one,'rb');

$data_one = fread($file_one,filesize($fileatt_one));

fclose($file_one);

 

$file_two = fopen($fileatt_two,'rb');

$data_two = fread($file_two,filesize($fileatt_two));

fclose($file_two);

 

$semi_rand = md5(time());

$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

$data_one = chunk_split(base64_encode($data_one));

$data_two = chunk_split(base64_encode($data_two));

 

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

$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";

 

$email_message .= "--{$mime_boundary}\n" ."Content-Type: {$fileatt_type_one};\n" ." name=\"{$fileatt_name_one}\"\n" ."Content-Transfer-Encoding: base64\n\n" .$data_one . "\n\n" ."--{$mime_boundary}--\n";

$email_message .= "--{$mime_boundary}\n" ."Content-Type: {$fileatt_type_two};\n" ." name=\"{$fileatt_name_two}\"\n" ."Content-Transfer-Encoding: base64\n\n" .$data_two . "\n\n" ."--{$mime_boundary}--\n";

 

$ok = @mail($email_to, $email_subject, $email_message, $headers);

if($ok) {

echo "<font face=verdana size=2>The file was successfully sent!</font>";

} else {

die("Sorry but the email could not be sent. Please go back and try again!");

}

 

}

?>

<form enctype="multipart/form-data" name="send" method="post" action="<?=$_SERVER['PHP_SELF']?>">

<input type="file" name="attachment[]" size="50" />

<input type="file" name="attachment[]" size="50" />

<input type="submit" name="send" value="Submit">

</form>

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/78355-send-multiple-attachments/
Share on other sites

  • 2 years later...

This what i did, the only problem with it now is that it is sending 2 email  each with one attachment. Is there a way we can have both coming into one email?

 

<?php
if(isset($_POST['send']))
{
$fileatt_one = $_FILES['attachment']['tmp_name'][0];
$fileatt_type_one = "application/octet-stream"; // File Type
$fileatt_name_one = $_FILES['attachment']['name'][0]; // Filename that will be used for the file as the attachment

$fileatt_two = $_FILES['attachment']['tmp_name'][1];
$fileatt_type_two = "application/octet-stream"; // File Type
$fileatt_name_two = $_FILES['attachment1']['name'][1]; // Filename that will be used for the file as the attachment

$email_from = "gikera@itura.net"; // Who the email is from
$email_subject = "Test"; // The Subject of the email
$email_txt = "Testing 123"; // Message that the email has in it
$email_to = "admin@itura.net"; // Who the email is too

$headers = "From: ".$email_from;

$file_one = fopen($fileatt_one,'rb');
$data_one = fread($file_one,filesize($fileatt_one));
fclose($file_one);

$file_two = fopen($fileatt_two,'rb');
$data_two = fread($file_two,filesize($fileatt_two));
fclose($file_two);

$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$data_one = chunk_split(base64_encode($data_one));
$data_two = chunk_split(base64_encode($data_two));

$headers .= "\nMIME-Version: 1.0\n" ."Content-Type: multipart/mixed;\n" ." boundary=\"{$mime_boundary}\"";
$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";

$email_message .= "--{$mime_boundary}\n" ."Content-Type: {$fileatt_type_one};\n" ." name=\"{$fileatt_name_one}\"\n" ."Content-Transfer-Encoding: base64\n\n" .$data_one . "\n\n" ."--{$mime_boundary}--\n";
$email_message .= "--{$mime_boundary}\n" ."Content-Type: {$fileatt_type_two};\n" ." name=\"{$fileatt_name_two}\"\n" ."Content-Transfer-Encoding: base64\n\n" .$data_two . "\n\n" ."--{$mime_boundary}--\n";

$ok = @mail($email_to, $email_subject, $email_message, $headers);
if($ok) {
echo "<font face=verdana size=2>The file was successfully sent!</font>";
} else {
die("Sorry but the email could not be sent. Please go back and try again!");
}

}
?>
<form enctype="multipart/form-data" name="send" method="post" action="<?=$_SERVER['PHP_SELF']?>">
<input type="file" name="attachment[]" size="50" /><br>
<input type="file" name="attachment1[]" size="50" /><br>
<input type="submit" name="send" value="Submit">
</form>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.