Forum Moderators: coopster

Message Too Old, No Replies

Simple IF statement question

         

dougmcc1

3:36 pm on Jul 30, 2003 (gmt 0)

10+ Year Member



if(mail($to, $subject, $body)) {
$sent = true;
}

Does this IF statement send the email to $to and make $sent = true or does it just say if the mail is sent then $sent = true?

dmorison

3:59 pm on Jul 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

From php.net:

mail() returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.

(my emphasis) - so $sent is true only if the local mail delivery agent has accepted the email, it _does not_ mean that $to is a valid email address...

Hope this helps.

dougmcc1

4:18 pm on Jul 30, 2003 (gmt 0)

10+ Year Member



So lets say that my code was:


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
}

dmorison

4:34 pm on Jul 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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
}
}

dougmcc1

6:07 pm on Jul 30, 2003 (gmt 0)

10+ Year Member



Wow thanks man. I don't understand what any of that means, but I get the general concept. But would you be willing to break it down for me? Otherwise I can look it up on my own. Thanks again.