4

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";
6
  • 1
    Use PHPmailer which is more advanced than simple mail() and easy to track errors Commented Mar 5, 2017 at 14:21
  • The mail might be send but gmail removed it (spam). In order to prevent it just use phpmailer Commented Mar 5, 2017 at 14:21
  • add this headers $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; and see Commented Mar 5, 2017 at 14:29
  • @AbdullaNilam No, It doesn't workout. Commented Mar 5, 2017 at 14:40
  • @LucaJung No log in the spam too... Commented Mar 5, 2017 at 14:40

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I'm running on the shared host.
There's a possibility that your host does not allow outgoing mail from mail
Oh, Let me check with the Hosting support team. But, As I mentioned, without this line $headers .= "Content-Type: text/html; charset=UTF-8\r\n"; mail() function works fine as a plain text.
I've run your code on my server (except the SQL query) and it works fine, with the html header line.
Thanks for your help. So, It must be Hosting server's problem. Let me check with them. Indeed, thanks @Nick Duncan
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.