0

I have this piece of test code to send an HTML email. For the longest time, it was sending, but not sending in HTML format. And now, it's not sending at all.

I've been comparing with code on the PHP manual, and with similar questions on this site, and I can't see anywhere that the code could be wrong. It's driving me crazy.

Why is this code not sending, and why is it not sending in HTML?

$to = "[email protected]";
$subject = "Confirmation code for registration";

$message = "<html>
  <head>";
$message .= '<meta http-equiv="content-type" content="text/html; charset=utf-8" />';
$message .= "<title>TITLE</title>
  </head>
  <body>";
$message .= "Thank you for registering! \r\n";
$message .= "
    </body>
</html>";

$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset: utf-8\r\n";
$headers .= "From: My Site <[email protected]>r\n";

$mailresult = mail($to, $subject, $message, $headers);
    echo "mail sent! ";
    echo $mailresult;

$mailresult comes back as 1, which I assume means success, but no mail is being recieved at the mail account specified.

11
  • Have you checked your Spam folder? Commented Mar 15, 2013 at 7:26
  • I would suggest using a mail library such as PHPMailer rather than the PHP mail() function. It has integrated debugging features and a simpler interface. Commented Mar 15, 2013 at 7:32
  • If it's not in your spam/junk folder, maybe there's something wrong with your SMTP, not necessarily your code Commented Mar 15, 2013 at 7:32
  • PHPMailer also makes it very easy to send email through SMTP such as gMail. This is valuable since it is difficult to tell if emails aren't being delivered because of an error in the code or because of server mail settings. Commented Mar 15, 2013 at 7:38
  • @BenM:Yes, it seems it's not coming through at all. The issue is, though, that I was receiving it fine and was only testing the HTML problem, and somewhere along the way I stopped receiving the mails. Commented Mar 15, 2013 at 7:39

3 Answers 3

4

According to php.net:

Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.

It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.

Have you configured a mail server to send the mail?

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

2 Comments

Awarded the green check for having pointed out that just because PHP says a mail is sent only means it has passed it on to the mail server, but not that the mail was processed through the mail server.
I'm glad I somehow helped you :)
1

Your code worked on my server perfectly but I received the email as Spam - so check your spam. And do your research to check if your server is configured properly.

Comments

-2

Try doing

mail($to, $subject, $message, $headers);

without $mailresult

1 Comment

$mailresult will just be a boolean representing the success or failure of mail. It has no affect on the sending of the email.

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.