Email attachment not working in Gmail

Hi,

I found this fantastic email attachment form that allows multiple attachments:

<?php

/* Mailer with Attachments */

$action = $_REQUEST['action'];
global $action;

function sendMail() { 

	$to_name = "Website Name";
	$from_name = stripslashes($_POST['Name']);
	$subject = "Application Form";
	$subject = stripslashes($subject);
	$body = stripslashes($_POST['body']);
	$to_email = "name@domain.com";

	//Let's start our headers 
	$headers  = "From: $from_name <" . $_POST['Email'] . ">\
"; 
	$headers .= "Reply-To: <" . $_POST['Email'] . ">\
";  
	$headers .= "MIME-Version: 1.0\
"; 
	$headers .= "Content-Type: multipart/related; type=\\"multipart/alternative\\"; boundary=\\"----=MIME_BOUNDRY_main_message\\"\
";  
	$headers .= "X-Sender: $from_name <" . $_POST['Email'] . ">\
"; 
	$headers .= "X-Mailer: PHP4\
"; 

	//if (checkbox_form.checkbox[counter].checked) 
	//Return-Receipt-To: x@x.com 
	//X-Confirm-Reading-To: x@x.com 
	//Disposition-Notification-To: x@x.com 
 
	$headers .= "X-Priority: 3\
"; //1 = Urgent, 3 = Normal 
	$headers .= "Return-Path: <" . $_POST['Email'] . ">\
";  
	$headers .= "This is a multi-part message in MIME format.\
"; 
	$headers .= "------=MIME_BOUNDRY_main_message \
";  
	$headers .= "Content-Type: multipart/alternative; boundary=\\"----=MIME_BOUNDRY_message_parts\\"\
";  
	$message  = "------=MIME_BOUNDRY_message_parts\
"; 
	$message .= "Content-Type: text/html; charset=\\"iso-8859-1\\"\
";  
	$message .= "Content-Transfer-Encoding: quoted-printable\
";  
	$message .= "\
";  
	/* Add our message, in this case it's plain text.  You could also add HTML by changing the Content-Type to text/html */ 
	$message .= "<h2>Test</h2>
<b>This is a test</b><br><br>
Test line 1<br>
Additional Test: $body"; 
	$message .= "\
";  
	$message .= "------=MIME_BOUNDRY_message_parts--\
";  
	$message .= "\
";  

	foreach($_FILES as $file => $value) { 
		$_tmpname = $_FILES[$file]['tmp_name']; 
		$_filename = $_FILES[$file]['name']; 
			if (is_uploaded_file($_tmpname)) { //Do we have a file uploaded? 
                $fp = fopen($_tmpname, "rb"); //Open it 
                $data = fread($fp, filesize($_tmpname)); //Read it 
                $data = chunk_split(base64_encode($data)); //Chunk it up and encode it as base64 so it can emailed 
                $message .= "------=MIME_BOUNDRY_main_message\
";  
                $message .= "Content-Type: application/octet-stream;\
\	name=\\"" . $_filename . "\\"\
"; 
                $message .= "Content-Transfer-Encoding: base64\
"; 
                $message .= "Content-Disposition: attachment;\
\	filename=\\"" . $_filename . "\\"\
\
"; 
                $message .= $data; //The base64 encoded message 
                $message .= "\
\
"; 
                fclose($fp); 
            } 
	} 

	$message .= "------=MIME_BOUNDRY_main_message--\
";  

	// send the message 

	mail("$to_name <$to_email>", $subject, $message, $headers); }

?>

<?php

switch ($action) {
  case "send":
    sendMail();
    break;
}
?>

The issue I am having is this: it works perfect in Hotmail & Outlook but it doesn’t work in Google mail - the attachments add for gmail, but the HTML email Test doesn’t show.

Had a play around for a couple hours now and had no joy, if anyone could spot a minor mistake or anything that would be great.

Cheers,

Paul

I renamed BOUNDRY to BOUNDARY and it works just the same - just a shame I can’t figure out how to get this working in Gmail - nice little script.

I have looked at the swiftmailer - it does look good but I just needed 1 simple form with minimal scripting as possible :slight_smile:

Cheers though :slight_smile:

I’m not sure about this, but shouldn’t it be MIME_BOUNDARY instead of MIME_BOUNDRY?
(or is that similar to the HTTP_REFERER issue?).

But to actually try to answer your question, it might be easier to use a mail library like Swift (http://swiftmailer.org/) to send mails with attachments. It works every time, in every client :wink:

what is your enctype?

Hi rguy84 - yeah I am adding the attachments via a form.

The attachment fields are named “attachment” “attachment2”

Does there need to be something specific for Google mail?

Cheers

enctype=“multipart/form-data”

Are you adding multiple attachments via a form? Is that set correctly?