Solved

PHP mail with attachment

Posted on 2013-05-29
2
306 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
Comment Utility
0
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 500 total points
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

There was an incident about the POP3 issue for the double read receipts and delivery receipts in Exchange 2013.  There was huge research been done and found solution for the duplicate mails. Especially when the user gets  duplicate mails.
Resolve Outlook connectivity issues after moving mailbox to new Exchange 2016 server
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

772 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

10 Experts available now in Live!

Get 1:1 Help Now