Jump to content

PHP Email with Multiple Attachments


jonoc33

Recommended Posts

Hey guys,

 

This is my entire script for sending multiple attachments.

 

<?php
session_start();

  $attachments = $_POST['theValue']; 

$client = $_POST['client'];
$email_from = $_POST['name']; // Who the email is from
$email = $_POST['email']; 
$email_subject = "Merit Requests - New Request"; // The Subject of the email 
$priority = $_POST['priority'];
$phone = $_POST['phone'];
$issue = $_POST['issue'];
$email_message = "Hello,<br>".$email_from." of ".$client." (<a href='mailto:".$email."'>".$email."</a>) has a request.<br><br>".$issue."<br><br>Priority: ".$priority."<br>Phone Number: ".$phone."<br><br>Thankyou.<br>Merit Requests"; // Message that the email has in it 
$email_to = "[email protected]"; // Who the email is too 
$headers = "From: ".$email;
$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-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_message . "\n\n"; 

// Attachment
if(strlen($_FILES['1']['name']) >> 0){
	$fileatt = $_FILES['1']['tmp_name']; // Path to the file 
	$fileatt_type = $_FILES['1']['type']; // File Type 
	$fileatt_name = $_FILES['1']['name']; // Filename that will be used for the file as the attachment 

	$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-Disposition: attachment;\n" . 
	//" filename=\"{$fileatt_name}\"\n" . 
	"Content-Transfer-Encoding: base64\n\n" . 
	$data . "\n\n" . 
	"--{$mime_boundary}\n"; 
	unset($data);
	unset($file);
	unset($fileatt);
	unset($fileatt_type);
	unset($fileatt_name);
}

// End Attachment

// Attachment
if(strlen($_FILES['2']['name']) >> 0){
	$fileatt = $_FILES['2']['tmp_name']; // Path to the file 
	$fileatt_type = $_FILES['2']['type']; // File Type 
	$fileatt_name = $_FILES['2']['name']; // Filename that will be used for the file as the attachment 

	$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-Disposition: attachment;\n" . 
	//" filename=\"{$fileatt_name}\"\n" . 
	"Content-Transfer-Encoding: base64\n\n" . 
	$data . "\n\n" . 
	"--{$mime_boundary}\n"; 
	unset($data);
	unset($file);
	unset($fileatt);
	unset($fileatt_type);
	unset($fileatt_name);
}

// End Attachment

// Attachment

if(strlen($_FILES['3']['name']) >> 0){
	$fileatt = $_FILES['3']['tmp_name']; // Path to the file 
	$fileatt_type = $_FILES['3']['type']; // File Type 
	$fileatt_name = $_FILES['3']['name']; // Filename that will be used for the file as the attachment 

	$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-Disposition: attachment;\n" . 
	//" filename=\"{$fileatt_name}\"\n" . 
	"Content-Transfer-Encoding: base64\n\n" . 
	$data . "\n\n" . 
	"--{$mime_boundary}\n"; 
	unset($data);
	unset($file);
	unset($fileatt);
	unset($fileatt_type);
	unset($fileatt_name);
}

// End Attachment

// Attachment

if(strlen($_FILES['4']['name']) >> 0){
	$fileatt = $_FILES['4']['tmp_name']; // Path to the file 
	$fileatt_type = $_FILES['4']['type']; // File Type 
	$fileatt_name = $_FILES['4']['name']; // Filename that will be used for the file as the attachment 

	$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-Disposition: attachment;\n" . 
	//" filename=\"{$fileatt_name}\"\n" . 
	"Content-Transfer-Encoding: base64\n\n" . 
	$data . "\n\n" . 
	"--{$mime_boundary}\n"; 
	unset($data);
	unset($file);
	unset($fileatt);
	unset($fileatt_type);
	unset($fileatt_name);
}

// End Attachment

// Attachment

if(strlen($_FILES['5']['name']) >> 0){
	$fileatt = $_FILES['5']['tmp_name']; // Path to the file 
	$fileatt_type = $_FILES['5']['type']; // File Type 
	$fileatt_name = $_FILES['5']['name']; // Filename that will be used for the file as the attachment 

	$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-Disposition: attachment;\n" . 
	//" filename=\"{$fileatt_name}\"\n" . 
	"Content-Transfer-Encoding: base64\n\n" . 
	$data . "\n\n" . 
	"--{$mime_boundary}\n"; 
	unset($data);
	unset($file);
	unset($fileatt);
	unset($fileatt_type);
	unset($fileatt_name);
}

// End Attachment


$ok = 1;
if(strlen($email_from) == 0){
	$ok = 0;
	$_SESSION['name'] = 1;
}
if(strlen($email) == 0){
	$ok = 0;
	$_SESSION['email'] = 1;
}
if(strlen($issue) == 0){
	$ok = 0;
	$_SESSION['issue'] = 1;
}
if(strlen($priority) == 0){
	$ok = 0;
	$_SESSION['priority'] = 1;
}
if($ok == 1) { 
	mail($email_to, $email_subject, $email_message, $headers); 

	mail($email, "Merit - Thankyou for your Request", "Hi ".$email_from.",\nThankyou for your request. You will get an e-mail within the next 24 hours or the next working day.\n\nThanks,\nMerit Technology Pty. Ltd.\nhttp://www.merit.com.au", "From: [email protected]");

	$_SESSION['sent'] = 1;
	unset($_SESSION['rem_name']);
	unset($_SESSION['rem_email']);
	unset($_SESSION['rem_phone']);
	unset($_SESSION['rem_issue']);
    	header("Location: index.php");
} else { 
	$_SESSION['problem'] = 1;
	// Remember what has been inputted
	$_SESSION['rem_name'] = $email_from;
	$_SESSION['rem_email'] = $email;
	$_SESSION['rem_phone'] = $phone;
	$_SESSION['rem_issue'] = $issue;
	header("Location: index.php");
} 

?>

 

It sends the email ok, it sends the attachments, but only 1 of them is the file that was inputted. The rest are text files with a bunch of code and content-type stuff with random letters and numbers.

 

How would I get this to work?

Link to comment
https://forums.phpfreaks.com/topic/101458-php-email-with-multiple-attachments/
Share on other sites

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.