10

I have the following email header:

$random_hash = md5(date('r', time()));
    $headers = array ('From' => $from,
   'To' => $to,
    'Return-Path' => 'info@example.com',
   'Subject' => $subject,
     'Content-Type'=>'multipart/alternative');

I want to send two versions of email in one email. One text and one html.

So I did this:

ob_start(); ?>
 --PHP-alt-<?php echo $random_hash; ?>
    Content-Type: text/plain; charset="iso-8859-1" 
    Content-Transfer-Encoding: 7bit
    Copy and paste: http://example.com/app_dl.php?app_id=<?php echo $_GET[app_id]; ?> to download your $app_name app \r\n
     Want to get tons more hot apps for free! anywhere anytime? Download our app on http://example.com  \r\n
     example.com team;

--PHP-alt-<?php echo $random_hash; ?>  
    Content-Type: text/html; charset="iso-8859-1" 
    Content-Transfer-Encoding: 7bit
     <p><a href='http://example.com/app_dl.php?app_id=<?php echo $_GET[app_id]; ?>'>Click Here</a> to download your <?php echo $app_name_app; ?></p>
     <p>Want to get tons more hot apps for free! anywhere anytime? Download our app on <a href='http://example.com'>example.com</a></p>
     <br/>
     <p>example.com team</p>";


     <?php 
      $bodyHTML =ob_get_clean();

But it doesnt seem to work well..and I wonder why!?!?

rid
  • 61,078
  • 31
  • 152
  • 193
Dmitry Makovetskiyd
  • 6,942
  • 32
  • 100
  • 160
  • Umm why do you create all the MIME manually and don't use a class for it where you simply specify HTML and text content? – ThiefMaster Apr 22 '12 at 12:16
  • 1.Define "doesn't work well". 2. "Content-Type" & "Content-Transfer-Encoding" should be part of the headers. – SimSimY Apr 22 '12 at 12:16
  • @TheSimon, they are part of the header, the problem is that the parts in the multipart message are not separated... – rid Apr 22 '12 at 12:17
  • What would be the possible solution to this , Radu..I took that code snippet from another site..It should work – Dmitry Makovetskiyd Apr 22 '12 at 12:19
  • possible duplicate of [PhpMailer vs. Swiftmailer?](http://stackoverflow.com/questions/303783/phpmailer-vs-swiftmailer) – mario Apr 22 '12 at 12:28
  • ?!?!? mario..why to downvote..no it isnt a duplicate – Dmitry Makovetskiyd Apr 22 '12 at 12:29
  • 1
    @mario, duplicate? What's PHPMailer and Swiftmailer got to do with this? – rid Apr 22 '12 at 12:29
  • @Radu: I don't really care. We have a few hundred duplicates on how to send HTML or /mixed email. If you don't like the duplicate I picked, you can always search for a better one. – mario Apr 22 '12 at 12:30
  • @DmitryMakovetskiyd: "This question shows no research effort." – mario Apr 22 '12 at 12:31
  • It does...I looked at the first result in google..It should work, like in the example they gave..but it didnt...I know the theory behind sending a text and html email..but why doesnt my code work. nice voting on close, mario..what a loser – Dmitry Makovetskiyd Apr 22 '12 at 12:34
  • 2
    @DmitryMakovetskiyd: You are mistaken. Stackoverflow is not a personal code fixing service. And just copy&pasting from the first Google result isn't much of research. You still didn't bother to search any further, nor did you actually try to understand the answer you got. – mario Apr 22 '12 at 12:36
  • I didnt copy and paste from google..read Radus post..My code is correct..But there is a small bug in it. I do understand the answer I got, stop mind reading... go troll some other post – Dmitry Makovetskiyd Apr 22 '12 at 12:39

2 Answers2

22

In order to create a multipart/alternative message, you need to specify a boundary and separate each part by that boundary. A good boundary string would be something that is highly unlikely to occur in the message part itself, such as a random string generated by sha1(uniqid()). For example:

MIME-Version: 1.0
Content-Type: multipart/alternative; boundary=c4d5d00c4725d9ed0b3c8b

--c4d5d00c4725d9ed0b3c8b
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

part1

--c4d5d00c4725d9ed0b3c8b
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

<b>part2</b>

--c4d5d00c4725d9ed0b3c8b--

This is specified in RFC 2046, section 5.1.4.

Community
  • 1
  • 1
rid
  • 61,078
  • 31
  • 152
  • 193
  • For a working example of this, see the section "Sending Email with an Attachment" in the article "[PHP: Sending Email (Text/HTML/Attachments)](http://webcheatsheet.com/PHP/send_email_text_html_attachment.php)." – creemama Apr 22 '12 at 12:28
  • Thats the article that I have been reading right now..see update – Dmitry Makovetskiyd Apr 22 '12 at 12:29
  • 1
    @DmitryMakovetskiyd, read the RFC or my answer, and you will see that you need to specify the boundary in the content type header, and you need a closing boundary as well. – rid Apr 22 '12 at 12:31
  • 1
    Okay, so what a I got wrong, is that i didnt close the boundary – Dmitry Makovetskiyd Apr 22 '12 at 12:44
  • 1
    @DmitryMakovetskiyd, that and the fact that you didn't specify the boundary in the content type header. – rid Apr 22 '12 at 12:45
  • I have Outlook 2010 and the message does not show up at all in it, though I can see my HTML portion just fine in Gmail. I can't seem to find this issue when searching. Any ideas on that by chance? – James Dec 17 '13 at 13:08
  • You can ignore my comment. I found the issue. I was using `\r\n` as everyone said I needed to, but I suppose my server setup is different because when I changed it to only `\n` it worked just fine. :/ – James Dec 17 '13 at 13:36
-2

I find using PHP_EOL instead of \r\n seems to work as well. Eg:

$headers = "Content-type: text/html; charset=UTF8" . PHP_EOL;
Kee
  • 7
  • 1
    PHP_EOL is based on the platform you're using. So if you take that same script that you're running on Linux and move it to a Windows box, it will then start sending "\n" instead of "\r\n" which is probably not what you want. – Sean Colombo Dec 14 '16 at 20:02