Link to home
Start Free TrialLog in
Avatar of Mark Brady
Mark BradyFlag for United States of America

asked on

HTML email sending plain text only php mail()

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


Avatar of DanielleFavreau
DanielleFavreau
Flag of United States of America image

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.
Avatar of Mark Brady

ASKER

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.
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.
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.
ASKER CERTIFIED SOLUTION
Avatar of jaxbrian
jaxbrian

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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