7

To make life difficult, the client I'm working for is using a really large yet old system which runs on PHP4.0 and they're not wanting any additional libraries added.

I'm trying to send out an email through PHP with both an attachment and accompanying text/html content but I'm unable to send both in one email.

This sends the 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 text/html:

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

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

$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;

What I need to know is how can I send an email with text/html in the email body and also add an attachment to it. I'm sure I'm missing something really simple here!

Thanks in advance.

11
  • 1
    Search for sending 'multipart' emails. One example is here. This example has one part text/html and text/plain, but you should be able to adapt it to your needs. Commented Jan 23, 2013 at 9:43
  • If you are able to do so update PHP. PHP4 is just too old and as far as I am informed not maintained anymore - so not even security fixes will be made for PHP4 branch. The last PHP4 update came in 2008! Commented Jan 23, 2013 at 9:43
  • 3
    To be perfectly frank, if a customer said to me "I'm using PHP 4.0 and I'm not prepared to upgrade", I would tell them that I'm not prepared to work on their project. Commented Jan 23, 2013 at 9:44
  • 1
    @Hikaru-Shindo: As I said, it's a really large system (~15,000 PHP files). The majority of it was created in 1998/9 and upgrading is pretty much out of the question as it would take far too long. Edit: I'm not saying that I like the fact they're using PHP4.0, but there's not really anything I can do about it. Commented Jan 23, 2013 at 9:47
  • 1
    Another problem you'll have is that PHP 4 itself is dangerously insecure, as is any operating system that still supports it. If they're using this for a publicly facing website, then they're insane, and also negligent of their customers's data. This system is in immediate danger of being hacked (if it hasn't been already). The danger for you is that if you're the last one to be working on it, you might end up getting the blame. For that reason alone, I recommend walking away now, before you get too involved. Commented Jan 23, 2013 at 9:56

2 Answers 2

14

Okay, I've finally cracked 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 <[email protected]>\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);
Sign up to request clarification or add additional context in comments.

Comments

2

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

I know you said you aren't allowed to use a third party library, but in this case, you would be crazy not to use one. We're talking about the difference between one day of work and a year; it's that big a 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 brittle, bug-ridden code that takes forever to get working reliably in all cases. This is why libraries like phpMailer exist.

So I suggest that regardless of what they want you to do, you should download phpMailer -- the PHP4 version is still available -- and see if it works for you. (The download page even still has the really old versions, so you should be able to go far enough back in time to find one that works with PHP 4.0. It shouldn't take you any time at all to get a simple demo up and running with it. Demonstrate that it works; that may well soften their stance against third party libraries.

(If it doesn't soften their stance, then there's not much hope of this project ever being completed in a sane amount of time. In that case, the best you can do is add a few extra zeros to your quote for the project price and grit your teeth)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.