Solved

PHP mail with attachment

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

867 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now