Drongo_III Posted March 3, 2013 Share Posted March 3, 2013 Hi Guys I am trying to setup an email template that contains both a html and plain text version in one. What follows is a simplified version but what I want to do is simply have both versions in an email template and then run a str_replace which will populate the template's place holders with the relevant data before sending - this is why I want everything in a single file. But before I get to that point I simply need to get the email working - however at present when I run the code below I just get blank output in the email body. If anyone could propose a reason why this is happening it would be appreciated. Incidentally I realise the mime boundary isn't ideal - this is just for testing. <?php $headers = "MIME-Version: 1.0\n"; $headers .= "Content-Type: multipart/related; boundary=\"boundaryMarker\"\n"; $headers .= "Content-Transfer-Encoding: 7bit\n"; $body = " --boundaryMarker\n Content-Type: text/plain; charset=\"charset=us-ascii\" \n Content-Transfer-Encoding: 7bit\n\n THIS IS PLAIN TEXT EMAIL \n\n --boundaryMarker\n \n\n Content-Type: text/html; charset=ISO-8859-1\n Content-Transfer-Encoding: 7bit\n\n <h1>THIS IS HTML VERSION</h1> \n\n --boundaryMarker--\n "; $to = 'MyEmail@Myemail.com'; $subject = 'This is a test message'; mail($to, $subject, $body, $headers); Quote Link to comment Share on other sites More sharing options...
Solution lachild Posted March 3, 2013 Solution Share Posted March 3, 2013 (edited) Found a couple of problems.. First, you really want to change your mime type From multipart/related TO: multipart/alternative. As these are not "related" to each other. Second... you need to get rid of the spacing and \n's from within the body. As you are in a quoted string the carrage returns and spacing will all be applyed in the message. Here's a working example with these changes: <?php $headers = "MIME-Version: 1.0\n"; $headers .= "Content-Type: multipart/alternative; boundary=\"boundaryMarker\"\n"; $headers .= "Content-Transfer-Encoding: 7bit\n"; $body = <<<MyEmailMessage --boundaryMarker Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit THIS IS PLAIN TEXT EMAIL --boundaryMarker Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit <h1>THIS IS HTML VERSION</h1> --boundaryMarker-- MyEmailMessage; $to = 'me@myemail.com'; $subject = 'This is a test message'; mail($to, $subject, $body, $headers); Edited March 3, 2013 by lachild Quote Link to comment Share on other sites More sharing options...
Christian F. Posted March 3, 2013 Share Posted March 3, 2013 Might it have something to do with the indenting, and copious amount of newlines, you've added to the mail..? Unfortunately I do not have the time (nor the desire, to be honest) to read up on the RFC to verify this. That said, it is worth looking into, and you should definitely read the RFC. Also, no need to use "\n" inside the string to add newlines. You're already doing that with the actual newlines. Quote Link to comment Share on other sites More sharing options...
Drongo_III Posted March 3, 2013 Author Share Posted March 3, 2013 Found a couple of problems.. First, you really want to change your mime type From multipart/related TO: multipart/alternative. As these are not "related" to each other. Second... you need to get rid of the spacing and \n's from within the body. As you are in a quoted string the carrage returns and spacing will all be applyed in the message. Here's a working example with these changes: <?php $headers = "MIME-Version: 1.0\n"; $headers .= "Content-Type: multipart/alternative; boundary=\"boundaryMarker\"\n"; $headers .= "Content-Transfer-Encoding: 7bit\n"; $body = <<<MyEmailMessage --boundaryMarker Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit THIS IS PLAIN TEXT EMAIL --boundaryMarker Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit <h1>THIS IS HTML VERSION</h1> --boundaryMarker-- MyEmailMessage; $to = 'me@myemail.com'; $subject = 'This is a test message'; mail($to, $subject, $body, $headers); Lachild thank you so much for your response. That fixed it all and now works perfectly. I'm now very happy As you've highlighted the mime type to me the penny kind of drops. So I guess 'multipart/related' might be used for things like attachments? Might it have something to do with the indenting, and copious amount of newlines, you've added to the mail..? Unfortunately I do not have the time (nor the desire, to be honest) to read up on the RFC to verify this. That said, it is worth looking into, and you should definitely read the RFC. Also, no need to use "\n" inside the string to add newlines. You're already doing that with the actual newlines. Christian - thanks for the tip. To be honest I was just working from examples on php.net which isn't overly expansive but I guess I'm looking in the wrong place for descriptive info on this. I shall check out RFC article. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted March 3, 2013 Share Posted March 3, 2013 (edited) You're welcome, and good luck on the project! Edited March 3, 2013 by Christian F. Quote Link to comment Share on other sites More sharing options...
lachild Posted March 3, 2013 Share Posted March 3, 2013 (edited) Drongo, Not a problem, glad I could help. As for attachments I don't think multipart/related is what you're looking for. multipart/related as I understand it are for emails which include different sections to complete an email... Like inline images and things like that. Or at least that how I understood https://tools.ietf.org/html/rfc2387. I believe for things like attachments you'll want to use multipart/mixed. Another thing you may want to consider is to use some prebuilt libraries. Might save you alot of greif as most of this kind of stuff has already been written and available for your use. I know things like Zend Framework have some built in email libraries that you can use, or another one that I've seen recomended quite a bit is PHPMailer. Might save you some headaches. Westin Edited March 3, 2013 by lachild Quote Link to comment Share on other sites More sharing options...
Drongo_III Posted March 3, 2013 Author Share Posted March 3, 2013 Drongo, Not a problem, glad I could help. As for attachments I don't think multipart/related is what you're looking for. multipart/related as I understand it are for emails which include different sections to complete an email... Like inline images and things like that. Or at least that how I understood https://tools.ietf.org/html/rfc2387. I believe for things like attachments you'll want to use multipart/mixed. Another thing you may want to consider is to use some prebuilt libraries. Might save you alot of greif as most of this kind of stuff has already been written and available for your use. I know things like Zend Framework have some built in email libraries that you can use, or another one that I've seen recomended quite a bit is PHPMailer. Might save you some headaches. Westin Ahh I see. I need to go do some reading me thinks! Heard of phpmailer but not used it - probably should check these out but I am the worst person for wanting to do it all myself to learn how it works ha! There's a control freak in all of us I guess Anyway thank you very, very much for the help! Quote Link to comment Share on other sites More sharing options...
Drongo_III Posted March 5, 2013 Author Share Posted March 5, 2013 Hi guys I implemented the code above as per the solution given but I seem to have a strange issue. when I set outlook 2007 to receive plain text email only and perform a test send outlook seems to ignore the plain text version in the email and simply defaults to a plain text version it seems to derive from the html version. has anyone seen this happen before and is to the code or something to do with outlook? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.