I am t开发者_JAVA技巧rying to send an email through php. I have tried sending to aol and hotmail accounts and no luck with either. Also firebug is returning me "mail sent" so i dont know what the problem is.
$to = '$em';
$subject = "Welcome";
$body = "Hi,\n\nTest";
$mail_sent = @mail($to, $subject, $body);
echo $mail_sent ? "Mail sent" : "Mail failed";
$em is an email address drawn from a database and i have checked and $em is defiantly equal to an email address
You have two big problems here. Let's break it down.
$to = '$em';
Variables are not interpolated inside of single-quoted strings. If you need to assign $to
to the value inside $em
, then ditch the quotes: $to = $em;
$mail_sent = @mail($to, $subject, $body);
echo $mail_sent ? "Mail sent" : "Mail failed";
The @
is an error silencing operator. Using it is bad practice, because it stops PHP from letting you know when something goes wrong. Ditch it.
Not that suppressing errors helps here, as mail()
is functionally retarded and never actually complains when something might have gone wrong.
Also firebug is returning me "mail sent" so i dont know what the problem is.
When mail()
returns true, as it has here, it means that PHP thinks that the mail was sent.
That doesn't mean it actually was sent. mail()
is a black box. Once it thinks it's succeeded, the mail is somewhere else and you have no way to check on it.
Please look into using your web hosting provider's SMTP server and a modern PHP mail library, like SwiftMailer. SwiftMailer is a bit more complex to use, but is pretty straight-forward to get working. The documentation is excellent.
By using a modern mail library that doesn't use mail
, you will be able to see any errors along the way and deal with them. It will also enable you to know that your message was sent, meaning you can follow up with your hosting provider and/or email provider to determine why the message was not delivered.
精彩评论