mail php attachment problem

I am trying to set up my script to send the content of a contact form via php mail.
When I add to attach the file attachment in the header of the email, then my message dissapears.
Any idea how I can work around this problem?
rivkamakAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
If I understand your design concept, the client is expected to fill in a contact form and upload a file.  Is that right?
0
Nrupendra Nath PanigrahiCommented:
Code moved to Code snippet.
<?php
    if(isset($_POST['submit']))
    {
    	$output = '<h1>Thanks for your file and message!</h1>';
    	$flags = 'style="display:none;"';

    	//Deal with the email
    	$to = 'xyz@example.com';
    	$subject = 'attached file';

    	$message = strip_tags($_POST['message']);
    	$attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name'])));
    	$filename = $_FILES['file']['name'];

    	$boundary =md5(date('r', time())); 

    	$headers = "From: admin@example.com\r\nReply-To: admin@example.com";
    	$headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\"";

    	$message="This is a multi-part message in MIME format.

--_1_$boundary
Content-Type: multipart/alternative; boundary=\"_2_$boundary\"

--_2_$boundary
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit

$message

--_2_$boundary--
--_1_$boundary
Content-Type: application/octet-stream; name=\"$filename\" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

$attachment
--_1_$boundary--";

    	mail($to, $subject, $message, $headers);
    }
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>MailFile</title>
</head>

<body>

<?php echo $output; ?>

<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" <?php echo $flags;?>>
<p><label for="message">Message</label> <textarea name="message" id="message" cols="20" rows="8"></textarea></p>
<p><label for="file">File</label> <input type="file" name="file" id="file"></p>
<p><input type="submit" name="submit" id="submit" value="send"></p>
</form>
</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rivkamakAuthor Commented:
Your script worked, and I have now discovered that somehow it has to do with my message content that isn't coming through.
If the $message  = "test";
then it comes through in the email.
When I switch the message to look like this, then no data comes through.
$message .= "Following is an application submitted\n\n";
    $message .= "Charity Name: ".($_POST['charityName'])."\n";
    $message .= "charity Website: ".($_POST['charityWebsite'])."\n";
    $message .= "contact Phone: ".($_POST['contactPhone'])."\n";
    $message .= "Email: ".($_POST['Email'])."\n";
    $message .= "Address: ".($_POST['Address'])."\n";
	    $message .= "Mission: ".($_POST['Mission'])."\n";
    $message .= "Taxid Number: ".($_POST['TaxidNumber'])."\n";
		$message .= "\n\nSubmitted on ".date("F j, Y, g:i a");

Open in new window

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Ray PaseurCommented:
Why not try printing the message to the browser window, so you can see what it contains?
0
rivkamakAuthor Commented:
the problem in my message above is the line that pulls the email. although I am not sure why.
If I take the post['email'] out , then I get the text.

#2, why do I get the attachement as a .dat file that doesn't open.
how can I just get the attachement as it is?
0
Ray PaseurCommented:
Something does not make sense here.  The original PHP script does not match the script fragment that contains information about charities.  Please, let's reorganize this question and its code snippets, so we can copy your code, install it on our own servers, and run it to see what it is doing.  We need you to create the SSCCE and post it here for us, thanks.
0
rivkamakAuthor Commented:
i changed the message to match the fields that I will need in the form.
So I realized that my file upload box has a different  name. I fixed that and now the upload works correctly. But again, the message isn't coming through.

	$message = "Following is an application submitted\n\n";
	  $message .= "message: ". strip_tags($_POST['message'])."\n";
	
  $message .= "Charity Name: ". strip_tags($_POST['charityName'])."\n";
  $message .= "Charity Name: ". strip_tags($_POST['charityName'])."\n";
    $message .= "charity Website: ". strip_tags($_POST['charityWebsite'])."\n";
   $message .= "contact Phone: ". strip_tags($_POST['contactPhone'])."\n";
//   $message .= "Email: ". strip_tags($_POST['Email'])."\n";
  $message .= "Address: ". strip_tags($_POST['Address'])."\n";
  $message .= "Mission: ". strip_tags($_POST['Mission'])."\n";
    $message .= "Taxid Number: ". strip_tags($_POST['TaxidNumber'])."\n";

Open in new window

0
Ray PaseurCommented:
Why not try printing the message to the browser window, so you can see what it contains?
If you do that, please post the message here so we can see what it contains, too.  You will need to use "view source" to copy the message -- the browser display will not be byte-by-byte accurate because of the way browsers render things like entities and whitespace.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.