?
Solved

PHP mail with attachment

Posted on 2013-05-29
2
Medium Priority
?
314 Views
Last Modified: 2013-06-17
I am trying to create an online form that sends out an email  In addition I want to be able to attach a file to the emailed form.

I have everything but the attachment working.

/////////////////////////////////  CODE FOR ATTAHMENT  /////////////////////////
$subject='New Vendor Application';
$textmessage=$email_message;
$name=$first_name;
$email='admin@me.com';
$recipient='rgranlund@me.com';
$mime_boundary=md5(time()); // unique boundary marker for your multi-part message
$mime_type="application/docx"; // change this to the correct mime type for your file
$filename=$prod_doc;
$document=$prod_doc; 
 
 $tmp_handle=fopen($document,'rb'); 
 $tmp_contnt=fread($tmp_handle,filesize($document)); 
 fclose($tmp_handle); 

$header = "From: ". $name . " <" . $email . ">\n";
$header .= "Reply-To: ". $name . " <" . $email . ">\n";
$header .= "Return-Path: <".$email.">\n"; 
$header .= "MIME-Version: 1.0\n"; 
$header .= "X-Mailer: PHP/" . phpversion()."\n";
$header.="Content-Type:multipart/mixed;boundary=\"".$mime_boundary."\"\n";
 
$email_message="--".$mime_boundary."\n";
$email_message.="Content-Type:text/plain;charset=iso-8859-1\n";
$email_message.="Content-Transfer-Encoding:7bit\n\n";
$email_message.=$textmessage."\n\n";

$email_message.="--".$mime_boundary."\n";
$email_message.="Content-Type:".$mime_type.";name=\"".$filename."\"\n";
$email_message.="Content-Transfer-Encoding:base64\n";
$email_message.="Content-Disposition:attachment;filename=\"".$filename."\"\n\n";
$email_message.=chunk_split(base64_encode($tmp_contnt))."\n\n";
$email_message.="--".$mime_boundary."--\n\n";

mail($recipient, $subject, $email_message, $header);

Open in new window


I know my problem is here:
$mime_type="application/docx"; // change this to the correct mime type for your file
$filename=$prod_doc;
$document=$prod_doc; 

Open in new window

0
Comment
Question by:rgranlund
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 10

Expert Comment

by:honestman31
ID: 39206441
0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 2000 total points
ID: 39206639
Your code basically works but you should be seeing several error messages about undefined variables.  Lines 3,4,9, and 10 are trying use variables that have no value in the code that you have shown.  For a test so you can work out the problems, comment out the last line and use a simple text file for the attachment so you can echo the $email_message to see what you have.  This is what I did.  Just make up a simple text file with like 4 short lines that you will be able to read on the screen.
/////////////////////////////////  CODE FOR ATTAHMENT  /////////////////////////
$subject='New Vendor Application';
$textmessage='This is the text message.';
$name='rgranlund';
$email='admin@me.com';
$recipient='rgranlund@me.com';
$mime_boundary=md5(time()); // unique boundary marker for your multi-part message
//$mime_type="application/docx"; // change this to the correct mime type for your file
$mime_type="text/plain"; // change this to the correct mime type for your file
$prod_doc="Youtextfile.txt";
$filename=$prod_doc;
$document=$prod_doc; 
 
 $tmp_handle=fopen($document,'rb'); 
 $tmp_contnt=fread($tmp_handle,filesize($document)); 
 fclose($tmp_handle); 

$header = "From: ". $name . " <" . $email . ">\n";
$header .= "Reply-To: ". $name . " <" . $email . ">\n";
$header .= "Return-Path: <".$email.">\n"; 
$header .= "MIME-Version: 1.0\n"; 
$header .= "X-Mailer: PHP/" . phpversion()."\n";
$header.="Content-Type:multipart/mixed;boundary=\"".$mime_boundary."\"\n";
 
$email_message="--".$mime_boundary."\n";
$email_message.="Content-Type:text/plain;charset=iso-8859-1\n";
$email_message.="Content-Transfer-Encoding:7bit\n\n";
$email_message.=$textmessage."\n\n";

$email_message.="--".$mime_boundary."\n";
$email_message.="Content-Type:".$mime_type.";name=\"".$filename."\"\n";
$email_message.="Content-Transfer-Encoding:base64\n";
$email_message.="Content-Disposition:attachment;filename=\"".$filename."\"\n\n";
$email_message.=chunk_split(base64_encode($tmp_contnt))."\n\n";
$email_message.="--".$mime_boundary."--\n\n";
echo "<pre>".$email_message."</pre>";

//mail($recipient, $subject, $email_message, $header);

Open in new window

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question