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.

James Donnelly
  • 117,312
  • 30
  • 193
  • 198
  • 1
    Search for sending 'multipart' emails. [One example is here](http://kevinjmcmahon.net/articles/22/html-and-plain-text-multipart-email-/). This example has one part text/html and text/plain, but you should be able to adapt it to your needs. – RickN Jan 23 '13 at 09: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! – Hikaru-Shindo Jan 23 '13 at 09: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. – SDC Jan 23 '13 at 09:44
  • @Hikaru-Shindo - its worse than that -- yes 4.4 was discontinued in 2008, but the last 4.0 release (ie the version he's working with) was in 2001. – SDC Jan 23 '13 at 09:46
  • 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. – James Donnelly Jan 23 '13 at 09:47
  • @RikkusRukkus: I've already tried that but I guess it won't hurt to take another look, thanks! – James Donnelly Jan 23 '13 at 09:48
  • 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. – SDC Jan 23 '13 at 09:56
  • "Well if it's not public it has not to be secured", it's always the same lame excuse. And then, all are *soooo* surprised if anything will ever happen (yes systems can be compromised from the inside!) Conclusion: If I had to work with PHP4 I would quit immediately! – dan-lee Jan 23 '13 at 10:01
  • @JamesDonnelly - well thank goodness for small mercies. I would still run a mile before working on it though. And if you do work on it, get it written into your contract that you're not going to be held responsible for any bugs or issues that arise after they've signed off your work. – SDC Jan 23 '13 at 10:04
  • @DanLee & SDC: I'm just a developer, I'm not responsible for choosing which clients the company I work for has. Saying these things to me isn't going to change anything. As I've said, upgrading is out of the question. – James Donnelly Jan 23 '13 at 10:12
  • I disagree. These things need to be said, and your management needs to hear it, because if they're charging for this project based on it being a small and simple bit of PHP work, and it turns out to take months because of the client's setup, then it will be your company that takes the financial hit if they haven't charged enough for the work. Your managers need to understand the reality of how much work this is going to be so that they can make informed decisions based on that. – SDC Jan 23 '13 at 10:36

2 Answers2

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 <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);
roberrrt-s
  • 7,249
  • 2
  • 41
  • 53
James Donnelly
  • 117,312
  • 30
  • 193
  • 198
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)

SDC
  • 13,745
  • 2
  • 31
  • 48