Jump to content

no body message in email with attachment


Betard

Recommended Posts

Hi Everyone,

 

I was wondering if A) I could get a second set of eyes on this, and B) if there is some way to test for these sort of issues so that I could try to figure it out in the future.

 

The assorted attachments seem to work out fine, but I can’t get the message to appear in the email body.  Upon submission of my form, I have the variables for the content print out and they do as expected, but for some reason I just can’t get it to appear within the email.

 

Unfortunately this doesn’t generate any errors, so I don’t really know how to test for them…

 

the code is on a unix box, and I am receiving the emails through Outlook 2007, using php5+

 

	function send_email_form () {

	$eol = "\r\n";

	// set variables
	$name_to = $_POST["name_to"];
	$email_to = $_POST["email_to"];

	$name_from = $_POST["name_from"];
	$email_from = $_POST["email_from"];

	$user_message = $_POST["user_message"];

	$formatted_message = "<html>\n 
		<body style=\"font-family:Verdana, Verdana, Geneva, sans-serif; font-size:14px; color:#666666;\">\n
			$user_message<br>\n
		</body>\n
		</html>\n";

	$filename = $_FILES['attachment']['name'];
	$filetype = $_FILES["attachment"]["type"]; 
	$filesize=$_FILES["attachment"]["size"]; 
	$tmp = $_FILES['attachment']['tmp_name'];

	// if (!($fp = fopen($tmp, 'rb'))) echo "File failed to open"; 

	$fp = fopen($tmp, "rb");
	$fdata = fread($fp, $filesize);
	$fdata = chunk_split(base64_encode($fdata)); // encode data into text form

	fclose($fp); 

	print("<p>tmp: $tmp</p>");
	print("<p>filename: $filename</p>");
	print("<p>fdata: $fdata</p>");
	print("<p>filetype: $filetype</p>");
	print("<p>filesize: $filesize</p>");

	// set unique mime boundary
	$mime_boundary = md5(time());

	// Headers for email

	$to = "$email_to";
	$subject = $_POST["subject"];

	$headers .= "From: ".$name_from." <".$email_from.">".$eol;
	$headers .= "Reply-To: ".$name_from." <".$email_from.">".$eol;
	$headers .= "Return-Path: ".$name_from." <".$email_from.">".$eol;    // these two to set reply address
	$headers .= "Message-ID: <".time()."-".$email_from.">".$eol;
	$headers .= "X-Mailer: PHP v".phpversion().$eol;          // These two to help avoid spam-filters	

	// Boundry for marking the split & Multitype Headers
	$headers .= 'MIME-Version: 1.0'.$eol;
	$headers .= "Content-Type: multipart/mixed; boundary=\"".$mime_boundary."\"".$eol.$eol;

	// Open first part of the mail
	$message = "--".$mime_boundary.$eol;

	// open a mime boundary for the text-html section
	$multialt_mime_boundary = $mime_boundary."_multialt";
	$message .= "Content-Type: multipart/alternative; boundary=\"".$multialt_mime_boundary."\"".$eol.$eol;

	// Text Email Section
	$message = "--".$multialt_mime_boundary.$eol;
	$message .= "Content-Type: text/plain; charset=UTF-8".$eol;
	$message .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
	$message .= "$user_message".$eol.$eol;

	// HTML Email Section
	$message .= "--".$multialt_mime_boundary.$eol;
	$message .= "Content-Type: text/html; charset=UTF-8".$eol;
	$message .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
	$message .= "$formatted_message".$eol.$eol;

	// close the text/html Message Boundary
	$message .= "--".$multialt_mime_boundary."--".$eol.$eol;


	// Attachment Start
	$message .= "--".$mime_boundary.$eol;
	$message .= "Content-Type: ".$filetype."; name=\"".$filename."\"".$eol;
	$message .= "Content-Transfer-Encoding: base64".$eol;
	$message .= "Content-Description: ".$filename.$eol;
	$message .= "Content-Disposition: attachment; filename=\"".$filename."\"".$eol.$eol;
	$message .= $fdata.$eol.$eol;

	$message .= "--".$mime_boundary."--".$eol.$eol;	


	// Send the email and display result message
	$mail_sent = mail($to, $subject, $message, $headers);

                // variables inserted here to just see that they are as expected
	echo $mail_sent ? "Your Message has been sent<br />$formatted_message<br />$user_message" : "Mail failed";

}

 

I have some print()s and echos just so I can verify certain variables while I test, but for the life of me I can't figure it out. 

 

To my knowledge my boundaries are set correctly, much like I am sure we all seem to think in these situations, it "should" work.

 

Appreciate any help I can get!

Link to comment
https://forums.phpfreaks.com/topic/97485-no-body-message-in-email-with-attachment/
Share on other sites

A) Should be available to you at a local fun store

B) Take a look at unit testing http://www.google.com/search?q=unit+testing

 

Are you kidding?  how is either supposed to help me out on this?  Neither A or B of your reply makes any sense.

 

I assume you are joking around with both replies... and trust me if I pair of those googley eyes glasses from a fun store would help I'd buy stocks in their company.

 

Sill looking for a solution to this issue, anyone?

Try this

 

http://www.daniweb.com/forums/thread60795.html

<?php

$fileatt = "mypdffile.pdf"; // Path to the file

$fileatt_type = "application/pdf"; // File Type

$fileatt_name = "mypdffile.pdf"; // Filename that will be used for the file as the attachment

 

$email_from = "[email protected]"; // Who the email is from

$email_subject = "Your attached file"; // The Subject of the email

$email_message = "Thanks for visiting mysite.com! Here is your free file.<br>";

$email_message .= "Thanks for visiting.<br>"; // Message that the email has in it

 

$email_to = $_POST['email']; // Who the email is to

 

$headers = "From: ".$email_from;

 

$file = fopen($fileatt,'rb');

$data = fread($file,filesize($fileatt));

fclose($file);

 

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

 

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

 

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

 

if($ok) {

echo "<font face=verdana size=2><center>You file has been sent<br> to the email address you specified.<br>

Make sure to check your junk mail!<br>

Click <a href=\"#\" onclick=\"history.back();\">here</a> to return to mysite.com.</center>";

 

} else {

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

}

?>

thanks for the relpy ansarka, I know there are lots out there already, but I am trying to "make" my own so I can learn more about it.  Unfortunately not much is learned by being defeated and resorting to copy/paste (although I am sure a lot of sanity is saved around the world through this method).

 

Mine is failing somehow, and I am just trying to figure out why/where I went wrong so that in the future I can look out for that mistake and/or even help others in the same situation.

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.