add attachment file to mail()

how to add attachment  file
using php mail()
I do not want to use phpmailer() which is a different program
LVL 1
rgb192Asked:
Who is Participating?
 
grahamnonweilerConnect With a Mentor Commented:
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
 
Dave BaldwinFixer of ProblemsCommented:
"php mail()" does not support attachments.
0
 
DrDamnitCommented:
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
rgb192Author Commented:
>>
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
 
grahamnonweilerCommented:
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
 
rgb192Author Commented:
<?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
 
grahamnonweilerCommented:
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
 
rgb192Author Commented:
I tried $document
''
'/'
'Folder location'
'FileName'
'Folder location / filename'

No attachment
0
 
grahamnonweilerCommented:
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
 
rgb192Author Commented:
>>
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
 
grahamnonweilerCommented:
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
 
rgb192Author Commented:
sorry

I meant to write swiftmailer.pdf
which is a file on the same directory as the emailSend.php
0
 
grahamnonweilerCommented:
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
 
grahamnonweilerCommented:
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
 
rgb192Author Commented:
<?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
 
rgb192Author Commented:
mail() can now send attachment
thank you
0
 
rgb192Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.