?
Solved

HTML email sending plain text only php mail()

Posted on 2011-09-26
6
Medium Priority
?
635 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 1500 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

807 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