Solved

PHP mail with attachment

Posted on 2013-05-29
2
312 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 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP query / monitor data from Telnet to MySQL 8 98
Multi line FPDF footer: 3 28
MySQL-Design Help 12 44
assigning javascript variable to php variable 8 43
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
What is Usenet? There are many different opinions on exactly what Usenet is an isn't. Many opinions are incorrect simply out of ignorance. The Wikipedia listing about Usenet does a good job of explaining it, so instead of repeating it all here I wi…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

726 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