SHAKTHI
SHAKTHI

Reputation: 41

PHP mail() Function not sending HTML

I'm trying to send HTML content through PHP mail() function. I don't know what is wrong with my code. I tried many ways, spent hours. but, nothing worked out. Can anyone tell me what is wrong with my code, please?

<?php 
$to = "[email protected]";
$today = date("Y/m/d");
$today = $today." 00:00";
$count_cash_inc = mysql_query("SELECT sum(income) as inc, sum(expense) as exp FROM journal_entry WHERE `date` >= '$today'");
$inc = mysql_fetch_array($count_cash_inc);
$income_c = $inc['inc'];
$epense_c = $inc['exp'];
$counter_cash = $income_c - $epense_c;
$subject = "Daily Report From Sri Sankalpa";
$headers = "From: [email protected]\r\n";
$headers .= "Reply-To: [email protected]\r\n";
$headers .= "CC: [email protected]\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$message = '<center><h1>Sri Sankalpa Daily Journal Report</h1>';
$message .= '<br><table><tr><td>Date</td><td>Total Income</td><td>Total Expense</td><td>Balance</td></tr>';
$message .= '<tr><td>'.date("d-m-Y").'</td><td>'.$income_c.'</td><td>'.$epense_c.'</td><td>'.$counter_cash.'</td></tr></table><br>';
$message .= '<p>To Know More... Please Click <strong><a target="_blank" href="http://srisankalpa.com/demo/journal_entry.php">HERE</a></strong></p></center>';
mail($to, $subject, $message, $headers);
?>

I tried removing \r,\r\n and changing charset=UTF-8 to charset=iso-8859-1. But nothing works. Looking forwarded for your help. Mail sending perfectly as a plain text without the below line

$headers .= "Content-Type: text/html; charset=UTF-8\r\n";

Upvotes: 4

Views: 8531

Answers (1)

Nick Duncan
Nick Duncan

Reputation: 829

I have run your script on my online server and it seems to work fine, albeit for me not including your SQL query and variables.

There could be a number of reasons as to why your email is not being sent.

Localhost mail server not set up

If you are running this script on your localhost, mail will most likely not work as you do not have a mail server set up on your localhost. Read this answer on setting up a mail server on your localhost (XAMPP).

Check your server logs

Have a look in your server logs for any errors that may have come up while trying to send the mail. The log files can normally be found in the root directory of your server within a logs folder or similar. This answer will help you identify the mail logs location.

Mail being blocked by host

Check with your host. Depending on who you host with, they may or may not be blocking outgoing emails. A lot of people use PHP's mail with bad intent, so some hosts may simply disable it. They would prefer you to use a SMTP server instead.

Your mail may have been marked as spam

Check your spam folder. Gmail is not a fan of emails being sent via mail and will most likely put your email in the spam folder.

Duplicate line breaks in your header string

In some cases, using \n\r could cause duplicate line breaks in your header string. Try using \r or \n instead.

Character encoding

Try switching from $headers .= "Content-Type: text/html; charset=UTF-8\r\n"; to $headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";

Better alternative than PHP's mail();

In order to send mail on your localhost you would be better off using something like PHPMailer. This will allow you to send mails via a SMTP server. There's a handy tutorial here.

Upvotes: 4

Related Questions