1

I am sending a very simple HTML message using PHP mail(). The purpose was to send a link that contained the user's email address and a verification code. When the user clicked the link, the site would receive the email address and code to verify that the user's email address was accurate.

At first, it worked, when it was nothing more than the HTML/BODY tags, a header, and the Verify Email link. I sent the email several times while debugging the verification process. Then I made what I considered to be minor changes, and it quit working. The minor changes are commented out as shown. I added an image and a couple lines of text.

It's not sending emails anymore, and I get no warnings or errors. I have removed the image, the extra text and commented out the addition of the link, but it will still not work. Can anyone see what I'm doing wrong? (I've changed names to protect the innocent.)

function sendEmail($email, $code) {

  $email = str_replace("@","%40",$email);
  $to = $email;
  $subject = 'Email Verification';

  $headers  = "From: " . strip_tags('someone@somewhere.com') . "\r\n";
  $headers .= "CC: \r\n";
  $headers .= "MIME-Version: 1.0\r\n";
  $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

  $message =  "<html><body>\r\n";
  $message .= "<h2>Thank you!</h2>\r\n";
  //$message .= '<img src="http://www.mytestwebsite.com/images/logo.png"><br>';
  //$message .= 'Thank you for registering an account with us. ';
  //$message .= 'Please verify your email address by clicking the button below.<br>';
  //$message .= '<a href="http://www.mytestwebsite.com/verifyemail.php?email='.$email.'&code='.$code.'">Verify Email</a>';
  $message .= "</body></html>\r\n";

  $message = wordwrap($message, 70, "\r\n");
  mail($to, $subject, $message, $headers);

}

How can I find out what's wrong with my setup? Is there a website that would display the errors in my code?

Thanks for your help! SH

Superhuman
  • 149
  • 1
  • 2
  • 13
  • Hmm.. I changed `$email` to my own personal email address and ran the code as it is (minus the function call) and I received an email. If you have access to your `php.ini` maybe you can turn on error messaging if it isn't already. [php.ini error msg](http://www.phpknowhow.com/configuration/php-ini-error-settings/) – lovermanthing Mar 27 '16 at 00:04
  • Good to know. Thanks for the info. The %40 function was one of many things I tried to get the verify to work. – Superhuman Mar 27 '16 at 00:33

1 Answers1

3

The only thing that seems to stop you from sending an email is:

$email = str_replace("@","%40",$email);

Because you are sending it to example%40hostname.com

Also to check if email is sent use:

if(@mail($to, $subject, $message, $headers))
{
  echo "Mail Sent Successfully";
}else{
  echo "Mail Not Sent";
}
Ivan Venediktov
  • 426
  • 2
  • 15