Jump to content

php mail - plain text and html


Drongo_III
Go to solution Solved by lachild,

Recommended Posts

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

 

Link to comment
Share on other sites

  • Solution

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 by lachild
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

 

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.

Link to comment
Share on other sites

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 by lachild
Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.