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 = "myemail@mysite.com";
$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 <registration@mysite.com>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.

Community
  • 1
  • 1
Questioner
  • 7,133
  • 16
  • 61
  • 94
  • Have you checked your Spam folder? – BenM Mar 15 '13 at 07: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. – None Mar 15 '13 at 07:32
  • If it's not in your spam/junk folder, maybe there's something wrong with your SMTP, not necessarily your code – TravellingGeek Mar 15 '13 at 07: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. – None Mar 15 '13 at 07: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. – Questioner Mar 15 '13 at 07:39
  • The code is working on my server, so nothing's wrong with the code.. maybe the server is having problems, how long have you been having the problem ? it may resend later - What I know is it looks like a server issue. – Alexi Akl Mar 15 '13 at 07:46
  • @AlexiAkl: Thank you for confirming the code works. I agree that it must be a server issue and I'll check with my server admin. – Questioner Mar 15 '13 at 11:17
  • @AlexiAkl: Actually, there is a typo in the code. See if you can find it ;) That typo was causing my mail server to fail. Did you copy my code exactly or just part of it? I'm curious as to how my code with its typo worked on your server. – Questioner Mar 15 '13 at 13:05
  • @DaveMG I copy pasted it and replaced myemail@mysite.com with my email.. that's all I did.. let me check if I can find the typo.. – Alexi Akl Mar 15 '13 at 13:15
  • @DaveMG Oh and I added the php beginning and ending tags , what is the typo?? you made me curious.. – Alexi Akl Mar 15 '13 at 13:18
  • @AlexiAkl: The typo is that there's no slash before the `r` in the line `From: My Site r\n";`. – Questioner Mar 15 '13 at 15:22

3 Answers3

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?

Community
  • 1
  • 1
TimeWasterNL
  • 102
  • 5
  • 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. – Questioner Mar 15 '13 at 11:18
  • I'm glad I somehow helped you :) – TimeWasterNL Mar 15 '13 at 14:20
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.

Alexi Akl
  • 1,934
  • 1
  • 21
  • 20
-2

Try doing

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

without $mailresult

Ramu PL
  • 9
  • 4
  • `$mailresult` will just be a boolean representing the success or failure of mail. It has no affect on the sending of the email. – None Mar 15 '13 at 07:31