[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

HTML email sending plain text only php mail()

Posted on 2011-09-26
6
Medium Priority
?
632 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

650 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