Solved

HTML email sending plain text only php mail()

Posted on 2011-09-26
6
613 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
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

708 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now