Forum Moderators: coopster
if(mail($to, $subject, $body)) {
echo "Mail was sent";
}
This does 1 of three things:
1. Sends the email and outputs "Mail was sent" if the email was delivered successfully.
2. Sends the email and outputs "Mail was sent" regardless of whether or not the recipient actually received the email.
3. Only outputs "Mail was sent" if the email was already sent.
I want number 1.
Do I need to do this?
mail($to, $subject, $body); //send the mail
if(mail($to, $subject, $body)) { //if sent
echo "Mail was sent"; //notify me
} else { //if not sent
echo "The mail was not sent"; //notify me
}
Do I need to do this?mail($to, $subject, $body); //send the mail
if(mail($to, $subject, $body)) { //if sent
echo "Mail was sent"; //notify me
} else { //if not sent
echo "The mail was not sent"; //notify me
}
No - that would send the mail twice.
The code you had first time is fine:
if(mail($to, $subject, $body)) {
echo "Mail was sent";
}
The only way you could do better than this is to check that $to is valid before you send. Here's my email address verification function, which checks the format and then the DNS mail exchange records for the address:
function isvalidemail( $email )
{
if (eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$", $email, $check))
{
if (getmxrr( substr(strstr($check[0], '@'), 1),$mxhosts))
{
return true;
}
}
return false;
}
You could combine this with your code to give:
if (!isvalidemail($to))
{
echo "Email address is not valid";
}
else
{
if(mail($to, $subject, $body))
{
echo "Mail was sent";
}
else
{
echo "The mail was not sent"; //notify me
}
}