简体   繁体   中英

PHP mail isn't sending - headers set incorrectly?

I have successfully sent mail using PHP's mail() function before, and for my password reset notification e-mail, I copied the syntax I was using elsewhere, but I guess I messed it up, as it's not arriving at its destination. Here is the code I'm using:

$headers = 'To:'.$email."\r\n";
$headers .= 'From: webmaster@aromaclear.co.uk'."\r\n";
$to = $email."\r\n";
$subject = 'AromaClear Password Reset Notification'. "\r\n";
$msg = 'From: AromaClear'."\r\n";
$msg .='Subject: Your New Password'. "\r\n";
$msg .= 'Message: Your new password is '.$newpass."\r\n";
$msg.= 'If you have received this e-mail in error, please ignore it.'. "\r\n";

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

Any thoughts?

Try looking at your server's mail logs to see why it isn't getting forwarded. Ex., it may be that this server's sendmail wants the -f flag for the From header instead of specifying it in the header text.

mail($to, $subject, $msg, $headers, "-f $from");

Also, you seem to be doing a lot of extra/weird work. This is a lot easier:

$subject = "AromaClear Password Reset Notification";
$headers = "From: webmaster@aromaclear.co.uk";
$msg = "Your new password is $newpass\r\nIf you have received this e-mail in error, please ignore it.\r\n.";

if(mail($email, $subject, $msg, $headers))
{
  //handle success
}
else
{
  //handle failure
}

Change style to your preference.

have you checked the return value of mail(). If it's not FALSE then it's accepted for delivery and the code is fine, but something is messed up somewhere else.

That looks fine to me, perhaps do

if (mail($to_email,$subject,$message, $headers))
    echo 'Success';
else
    echo 'Error';
}

That might let you know if it's trying to send at all.

Just don't add "\\r\\n" everywhere, use it only to separate headers. In the message you can use only \\n, it will work. And at the end of the subject and receiver there's no need for "\\r\\n".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM