Using mail () to send an attachment AND text / html in an email in PHP4

To make life difficult, the client I'm working on uses a really big, but old system that runs on PHP4.0 and they don’t want to add additional libraries.

I am trying to send email via PHP with an attachment and accompanying text / html content, but I cannot send them in a single email.

Sends an attachment:

$headers = "Content-Type: text/csv;\r\n name=\"result.csv\"\r\n Content-Transfer-Encoding: base64\r\n Content-Disposition: attachment\r\n boundary=\"PHP-mixed-".$random_hash."\""; $output = $attachment; mail($emailTo, $emailSubject, $output, $headers); 

This sends the text / html:

 $headers = "Content-Type: text/html; charset='iso-8859-1'\r\n"; $output = $emailBody; // $emailBody contains the HTML code. 

This sends the attachment containing the text / html, along with the contents of the attachment:

 $headers = "Content-Type: text/html; charset='iso-8859-1'\r\n".$emailBody."\r\n"; $headers = "Content-Type: text/csv;\r\n name=\"result.csv\"\r\n Content-Transfer-Encoding: base64\r\n Content-Disposition: attachment\r\n boundary=\"PHP-mixed-".$random_hash."\""; $output = $attachment; 

I need to know how I can send an email with the text / html to the email body and also add an attachment to it. I am sure that I am missing something very simple here!

Thanks in advance.

+7
source share
2 answers

Ok, I finally hacked it! For reference:

 $emailBody .= "<html><body>Blah</body></html>"; $emailSubject = "Subject"; $emailCSV = "\"Col1\", \"Col2\"\r\n"; // etc. $attachment = $emailCSV; $boundary = md5(time()); $header = "From: Name < address@address.com >\r\n"; $header .= "MIME-Version: 1.0\r\n"; $header .= "Content-Type: multipart/mixed;boundary=\"" . $boundary . "\"\r\n"; $output = "--".$boundary."\r\n"; $output .= "Content-Type: text/csv; name=\"result.csv\";\r\n"; $output .= "Content-Disposition: attachment;\r\n\r\n"; $output .= $attachment."\r\n\r\n"; $output .= "--".$boundary."\r\n"; $output .= "Content-type: text/html; charset=\"utf-8\"\r\n"; $output .= "Content-Transfer-Encoding: 8bit\r\n\r\n"; $output .= $emailBody."\r\n\r\n"; $output .= "--".$boundary."--\r\n\r\n"; mail($emailTo, $emailSubject, $output, $header); 
+14
source

Trying to send attachments using the PHP mail() function is an exercise in frustration; simple, just not worth the effort. I would never suggest even trying it, even in current versions of PHP. I would always use a library like phpMailer.

I know that you said that you are not allowed to use a third-party library, but in this case you would be crazy not to use it. We are talking about the difference between one day of work and a year; this is a big deal. The PHP mail() function is incredibly difficult to work with, and trying to write code with it to send attachments from scratch will leave you with a fragile error code that will always remain reliable in all cases. This is why there are libraries like phpMailer.

Therefore, I suggest that no matter what they want from you, you should download phpMailer - the version of PHP4 is still available - - and see if it works for you. (There are still really old versions on the download page, so you have to go far enough back to find one that works with PHP 4.0. You don’t need to take the time to get a simple demo and Demonstrate that it works, which can soften them position regarding third-party libraries.

(If this does not soften their position, then there is little hope that this project will ever be completed in a reasonable amount of time. In this case, the best thing you can do is add a few extra zeros to your quote for the price of the project and sand of your teeth)

+2
source

All Articles