Solved

PHP mail with attachment

Posted on 2013-05-29
2
309 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
2 Comments
 
LVL 10

Expert Comment

by:honestman31
ID: 39206441
0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
When you’re making plans to join the modern business race, you should analyze various details that may affect your results. Nowadays, millions of businesses are trying to grow into established and appreciated professional enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

785 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