[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

add attachment file to mail()

Posted on 2012-08-17
18
Medium Priority
?
773 Views
Last Modified: 2012-08-29
how to add attachment  file
using php mail()
I do not want to use phpmailer() which is a different program
0
Comment
Question by:rgb192
18 Comments
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 38307152
"php mail()" does not support attachments.
0
 
LVL 32

Expert Comment

by:DrDamnit
ID: 38307173
You will have to use phpmailer.

Your only other option is (if you have access to the shell) install mail utils, and use exec() to run mailx or something similar to send out the email and then attach an attachment to it.
0
 

Author Comment

by:rgb192
ID: 38307174
>>
Your only other option is (if you have access to the shell) install mail utils, and use exec() to run mailx or something similar to send out the email and then attach an attachment to it.

could you explain more
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 16

Expert Comment

by:grahamnonweiler
ID: 38307267
You can use the PHP mail() function to send attachments, but you have to create the message in "multi-part mime message" format.

Meaning, the mail() function allows you to specify:

who to send mail to
the subject line
the message body
the headers

To create and send a multi-part mime message (ie. a message with attachements) then you will have to manually (programmatically) create the "headers" and the "message body".

The actual attachment will have to converted to base64 encoded textual content, that will be appended to the message body.

The message will have to have a unique identifier, that is stated in the headers as the "boundary" and then used within the message body with special markers to allow the receiving MTA to decode the attachment.

A rough example of how to do this with php (please note this is meant only as an example):

<?

$mime_boundary=md5(time()); \\ unique boundary marker for your multi-part message
$mime_type="application/pdf"; \\ change this to the correct mime type for your file

\\ $filename = the name of your attached file
\\ $document = the path to the actual file you wish to attach
\\ $sendto = the eail address you are sending to
\\ $textmessage = the textual part of your message

$mlsubj="This is my subject"; \\ replace as needed

$headers="To:Someone<someone@company.com>\n"; \\ replace these as needed
$headers.="From:MyEmail<me@company.com>\n"; \\ replace these as needed
$headers.="Date: ".date("r")."\n";
$headers.="MIME-Version:1.0\n";
$headers.="Content-Type:multipart/mixed;boundary=\"".$mime_boundary."\"\n";

$msg_body="--".$mime_boundary."\n";
$msg_body.="Content-Type:text/plain;charset=iso-8859-1\n";
$msg_body.="Content-Transfer-Encoding:7bit\n\n";
$msg_body.=$textmessage."\n\n";

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

$sendmessage=mail($sendto, $mlsbj, $msg_body, $headers);

?>
0
 

Author Comment

by:rgb192
ID: 38307601
<?php
$subject='subject';
$body='body';
$name='name';
$email='test@gmail.com';
$recipient='test@gmail.com';

$header = "From: ". $name . " <" . $email . ">\r\n";
$header .= "Reply-To: ". $name . " <" . $email . ">\r\n";

$header .= "Return-Path: <".$email.">\n"; 
$header .= "MIME-Version: 1.0\n"; 
$header .= "Content-Type: text/html; charset=ISO-8859-1\n";

$header .= "X-Mailer: PHP/" . phpversion()."\r\n";

$mime_boundary=md5(time()); // unique boundary marker for your multi-part message
$mime_type="application/pdf"; // change this to the correct mime type for your file
 //$filename = the name of your attached file
 // $document = the path to the actual file you wish to attach
$filename="Swiftmailer.pdf";
//$document=" /home/content/27/html/";
$document=""; 
 

$msg_body="--".$mime_boundary."\n";
$msg_body.="Content-Type:text/plain;charset=iso-8859-1\n";
$msg_body.="Content-Transfer-Encoding:7bit\n\n";
$msg_body.=$textmessage."\n\n";

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



mail($email, $subject, $msg_body, $header);
?>

Open in new window





instead of seeing attachment

I see this in the body

--d082f5d892e32d7152ed016100f3d1df Content-Type:text/plain;charset=iso-8859-1 Content-Transfer-Encoding:7bit --d082f5d892e32d7152ed016100f3d1df Content-Type:application/pdf;name="Swiftmailer.pdf" Content-Transfer-Encoding:base64 Content-Disposition:attachment;filename="Swiftmailer.pdf" IC9ob21lL2NvbnRlbnQvMjcvODkzOTEyNy9odG1sLw== --d082f5d892e32d7152ed016100f3d1df--
0
 
LVL 16

Expert Comment

by:grahamnonweiler
ID: 38307620
The attachment will not be "human" readable, it is base64 encoded!

Just make sure the actual PDF is correctly defined in terms of its path and location so that it can be read by the script, and in turn encoded.

For instance:

/path1/path2/document.pdf

Send an email to yourself and check the attachment is readable. If it is not, then it is because the script was not able to find the file to encode.
0
 

Author Comment

by:rgb192
ID: 38307699
I tried $document
''
'/'
'Folder location'
'FileName'
'Folder location / filename'

No attachment
0
 
LVL 16

Expert Comment

by:grahamnonweiler
ID: 38307869
It may be that your environent requires you to read the file contents in to a variable first before encoding. You will see below there is 3 new lines that read the contents of the pdf file in to a string.

Thus try:


<?

$mime_boundary=md5(time()); \\ unique boundary marker for your multi-part message
$mime_type="application/pdf"; \\ change this to the correct mime type for your file

\\ $filename = the name of your attached file
\\ $document = the path to the actual file you wish to attach
\\ $sendto = the eail address you are sending to
\\ $textmessage = the textual part of your message

\\ go get file content in to a string

      $tmp_handle=fopen($document,'rb');
      $tmp_contnt=fread($tmp_handle,filesize($document));
      fclose($tmp_handle);


$mlsubj="This is my subject"; \\ replace as needed

$headers="To:Someone<someone@company.com>\n"; \\ replace these as needed
$headers.="From:MyEmail<me@company.com>\n"; \\ replace these as needed
$headers.="Date: ".date("r")."\n";
$headers.="MIME-Version:1.0\n";
$headers.="Content-Type:multipart/mixed;boundary=\"".$mime_boundary."\"\n";

$msg_body="--".$mime_boundary."\n";
$msg_body.="Content-Type:text/plain;charset=iso-8859-1\n";
$msg_body.="Content-Transfer-Encoding:7bit\n\n";
$msg_body.=$textmessage."\n\n";

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

$sendmessage=mail($sendto, $mlsbj, $msg_body, $headers);

?>
0
 

Author Comment

by:rgb192
ID: 38315016
>>
Suggestion: Why not just store the PDF on the server file system and send the client a link to open/download the PDF?  Poof!  No attachment problems, and the client will never have to call you saying, "I accidentally deleted my PDF - can you send me another?"


because my boss wants me to send attachments.  I do agree with your idea.





<?php
$subject='subject';
$body='body';
$name='name';
$email='test@gmail.com';
$recipient='test@gmail.com';

$header = "From: ". $name . " <" . $email . ">\r\n";
$header .= "Reply-To: ". $name . " <" . $email . ">\r\n";

$header .= "Return-Path: <".$email.">\n"; 
$header .= "MIME-Version: 1.0\n"; 
$header .= "Content-Type: text/html; charset=ISO-8859-1\n";

$header .= "X-Mailer: PHP/" . phpversion()."\r\n";

$mime_boundary=md5(time()); // unique boundary marker for your multi-part message
$mime_type="application/pdf"; // change this to the correct mime type for your file
 //$filename = the name of your attached file
 // $document = the path to the actual file you wish to attach
$filename="Swiftmailer.pdf";
//$document=" /home/content/27/html/";
$document="Swiftmailer.php"; 

      $tmp_handle=fopen($document,'rb'); 
      $tmp_contnt=fread($tmp_handle,filesize($document)); 
      fclose($tmp_handle); 
 

$msg_body="--".$mime_boundary."\n";
$msg_body.="Content-Type:text/plain;charset=iso-8859-1\n";
$msg_body.="Content-Transfer-Encoding:7bit\n\n";
$msg_body.=$textmessage."\n\n";

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



mail($email, $subject, $msg_body, $header);
?>

Open in new window



I still get code that appears in the body of email and no attachment
0
 
LVL 16

Expert Comment

by:grahamnonweiler
ID: 38315063
If there is no attachment then it means your script is not able to read the PDF file. From your changes to the script above it looks like you are trying to create a PDF file with the script Swiftmailer.php - and that is probably sending headers but no filesize, and thus there is no file for the fread to actually work with.

You should create the PDF file before attempting to attach it!

The PDF file can always be deleted after you have sent the email.

Also, when you are troubleshooting a script it is good practice to try and isolate where the problem might be, in this case you should make changes to the script to write the contents of variables to a flat text file as it progresses. Then you could look at the flat text files to see what is actually happening during the process.

Further, if you base64_decode the text that is appearing in the output from email script, you may find that it is the headers or error message being sent by your swiftmailer.php script.
0
 

Author Comment

by:rgb192
ID: 38315286
sorry

I meant to write swiftmailer.pdf
which is a file on the same directory as the emailSend.php
0
 
LVL 16

Expert Comment

by:grahamnonweiler
ID: 38315326
Have you checked that the mailing script can read the PDF file in to the variable before encoding it?

You do that by writing the content of the variable to another flat file and then comparing that with the original.
0
 
LVL 16

Expert Comment

by:grahamnonweiler
ID: 38315336
I have just noticed that you have not copied the original PHP example I gave you correctly.

In line 13 you have:

$header .= "Content-Type: text/html; charset=ISO-8859-1\n";

When it should be:

$headers.="Content-Type:multipart/mixed;boundary=\"".$mime_boundary."\"\n";


Additionally the $mime_boundary must be defined BEFORE you create the headers as it is included in the HEADERS.

Correct your script and try again.
0
 

Author Comment

by:rgb192
ID: 38318320
<?php
$subject='subject';
$body='body';
$name='name';
$email='test@gmail.com';
$recipient='test@gmail.com';

$mime_boundary=md5(time()); // unique boundary marker for your multi-part message
$mime_type="application/pdf"; // change this to the correct mime type for your file

 //$filename = the name of your attached file
 // $document = the path to the actual file you wish to attach
$filename="Swiftmailer.pdf";
$document="Swiftmailer.pdf"; 
 
 
       $tmp_handle=fopen($document,'rb'); 
      $tmp_contnt=fread($tmp_handle,filesize($document)); 
      fclose($tmp_handle); 


$header = "From: ". $name . " <" . $email . ">\r\n";
$header .= "Reply-To: ". $name . " <" . $email . ">\r\n";

$header .= "Return-Path: <".$email.">\n"; 
$header .= "MIME-Version: 1.0\n"; 
$headers.="Content-Type:multipart/mixed;boundary=\"".$mime_boundary."\"\n";

$header .= "X-Mailer: PHP/" . phpversion()."\r\n";
 
 

$msg_body="--".$mime_boundary."\n";
$msg_body.="Content-Type:text/plain;charset=iso-8859-1\n";
$msg_body.="Content-Transfer-Encoding:7bit\n\n";
$msg_body.=$textmessage."\n\n";

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



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

echo '<br>'.$msg_body;
echo '<br>finished';
?>

Open in new window





no attachment
0
 
LVL 16

Accepted Solution

by:
grahamnonweiler earned 2000 total points
ID: 38319019
Again, unfortunately you did not correctly copy the original script I provided. You have renamed variables (header for headers, body instead of textessage, and so on) and also you have an error n line 42 where you have not implemented the change I provided for reading the file in to a variable.

It should read: $msg_body.=chunk_split(base64_encode($tmp_contnt))."\n\n";d:

Below you will find the corrected script. It is important to read through code so that you understand what it is doing. Make the corrections and try again.

---- Corrected Script --

<?php
$subject='subject';
$textmessage='body';
$name='name';
$email='test@gmail.com';
$recipient='test@gmail.com';
$mime_boundary=md5(time()); // unique boundary marker for your multi-part message
$mime_type="application/pdf"; // change this to the correct mime type for your file
$filename="Swiftmailer.pdf";
$document="Swiftmailer.pdf"; 
 
 $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";
 
$msg_body="--".$mime_boundary."\n";
$msg_body.="Content-Type:text/plain;charset=iso-8859-1\n";
$msg_body.="Content-Transfer-Encoding:7bit\n\n";
$msg_body.=$textmessage."\n\n";

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

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

echo (str_replace("\n", "<br>", $header));
echo ("<br>");
echo (str_replace("\n", "<br>", $msg_body));
echo ("<br>finished");
?>

Open in new window

0
 

Author Closing Comment

by:rgb192
ID: 38333447
mail() can now send attachment
thank you
0
 

Author Comment

by:rgb192
ID: 38347932
I have a related question
of embeding (not attach) image in php mail()

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_27847224.html
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month18 days, 3 hours left to enroll

830 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