Solved

HTML email sending plain text only php mail()

Posted on 2011-09-26
6
619 Views
Last Modified: 2013-12-13
I have a function as below

function sendMail(){
$to     = "someone@happy.com";
$subject = "HTML mail test";
$emailBody = "<p>Hello Sir</p><p><b>WUZZUP</b></p>This is all to good....";

$headers  = "Sent From: noreply@happy.com\r\n";
$headers .= "Reply-To: noreply@happy.com\r\n";
$headers .= "Content-type: text/html\r\n";

#sendMail
$mail_sent  = mail( $to, $subject, $emailBody, $headers );
}

When I run it, it only sends plain text email. I have never had this problem before but I can put an HTML table in a function and it sends the proper html table. Can anyone see what is wrong with this code?

Here is what I get in the email.....

Sent From: noreply@happy.com
Reply-To: noreply@happy.com
Content-type: text/html

<p>Hello Mark</p><p><b>WUZZUP</b></p>This is all to good....


0
Comment
Question by:Mark Brady
  • 3
  • 2
6 Comments
 
LVL 7

Expert Comment

by:DanielleFavreau
ID: 36707479
Your mail clause should be:

mail($RecipientEmail,$Subject,$EmailBody,"From: ".$SendingEmail."\nContent-Type: text/html; charset=iso-8859-1","-femail@domain.com");

The Content-Type will make it HTML and the -femail@domain.com will make it so it doesn't show which domain it's coming from.  That isn't necessary but it helps if you forward your email through a shared hosting account.
0
 
LVL 20

Author Comment

by:Mark Brady
ID: 36707516
Yes I know this but my question is, why am I all of a sudden getting plain text? I already have the correct format (sendto,subject,message,headers) and I have the content type set to text/html and I did try it with a charset statement but no luck. The thing is, I have written these type of functions in at least a dozen website all which work perfectly. It is only the website I'm working on that decided not to send HTML.

I've tried using different browsers on 4 different computers - no change. I must be missing something.
0
 
LVL 7

Expert Comment

by:DanielleFavreau
ID: 36707523
Have you been able to successfully send HTML mail from that particular server previously?  If it's a Windows server they are notorious for blocking several functions in PHP Mail.

Try your code on another server where you know it has worked before and see if it works.  If it doesn't then you know your code has a problem.  If it works then you know your other server has a problem.
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 20

Author Comment

by:Mark Brady
ID: 36708253
Yes as a matter of fact I have several websites on the same server ... or at least the same hosting account that all send html fine. Actually, yes they are the same server because they are all in folders under my main account. So, must be the code. I will try a few other ways tomorrow like copying another function from one of my other sites into this script and see if it works.
0
 
LVL 2

Accepted Solution

by:
jaxbrian earned 500 total points
ID: 36711693
it is something in your headers. I have not narrowed it down yet but I replaced yours with the ones below and got it to run.
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// I broke the code down and got this to work, should just be able to add the function back to it.

$to     = 'someone@happy.com';
$subject = "HTML mail test";
$emailBody = "<p>Hello Sir</p><p><b>WUZZUP</b></p>This is all to good....";

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

mail($to, $subject, $emailBody, $headers);

?>

Open in new window

0
 
LVL 20

Author Closing Comment

by:Mark Brady
ID: 36713577
Yes you are correct. Actually, I found out last night that my headers were causing the problem. I don't follow the reason why but it was definitely the hears.
I changed the order of the header from this

$headers  = "Sent From: ".$this->company."\r\n";
$headers .= "Reply-To: ".$loc_email."\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";


To this:

$headers  = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "Sent From: ".$this->company."\r\n";
$headers .= "Reply-To: ".$loc_email."\r\n";

and it worked! Putting the content and mime type first fixed the issue. I would not have guessed that in a million years but just thought I would try it for fun.

The only thing I can think of is the variable $this->company    or $loc_email   was stopping the headers from being parsed properly for some reason although both of those variable are replaced in the email with their respective values so that doesn't really make sense. Oh well, it works now - Thanks
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question