tags:

views:

356

answers:

5

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: [email protected]'."\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?

+3  A: 

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: [email protected]";
$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.

Sam Bisbee
I changed the code to this, and added a different key-value pair to the query string depending for success or failure and it seems to have passed (I also added the optional -f parameter), but I still don't get the e-mail. Have checked the database and the e-mail address and username are right, as they have been for weeks of testing). Still nothing. Strange.
Then my guess is that there's something going on lower in the stack. You really need to take a look at your system's mail log (usually /var/log/mail) and see if the mail is even getting pushed. If you don't see your mail showing up, then PHP is probably misconfigured for sendmail (not pointing to where sendmail lives?). Maybe your sendmail is misconfigured? Maybe your ISP is blocking SMTP relay?
Sam Bisbee
I have sent mail before successfully from the same server, which is what makes this particularly odd. I sent some earlier today, as a matter of fact, and I have another script that works, sending a large HTML encoded message and the original syntax I posted here (except $msg was just one enormous string of CSS and HTML).
That doesn't mean you looked at the mail logs. :-)Were you sending to a different destination? Try running your working script with the same e-mails and content that you're using now. If PHP is saying that the e-mail was sent successfully, then the problem lies somewhere lower in the stack - sendmail. My guess is that either your box or the one you're relaying to is blocking the relay.
Sam Bisbee
I can't find any logs on the server using FileZilla. Can I just add one, and if so, do I need to restart the server for it to work? I don't have any control over the server. I have added an error log for PHP before, though, so I guess it doesn't need to be restarted. How do I do it? Sorry, n00b.
No problem. :-) My guess is that you'll need to call whoever is hosting your server to get access to the logs. They may not let you see the log if you're on a shared environment though, so you may have to have them watch the logs and send you entries accordingly. Depending on the quality of tech you reach, they may be able to help you debug your problem.
Sam Bisbee
Thanks, I'll try that :-)
+1  A: 

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.

cheesysam
+2  A: 

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.

rubayeet
+1  A: 

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".

p4bl0
A: 

Syntax errors did abound, but now they have been found, and things were swapped around, and this is beggining to sound like a...poem. Dangit. Fixed anyway! Cheers, guys.