Home > database >  PHP mail successful with empty to parameter
PHP mail successful with empty to parameter

Time:06-30

I am learning PHP and from what I understand the mail function has a to parameter that needs to comply with a certain string format - php doc. I have read that if I parse an empty string then the mail function will return false (not 1). However, when I try this the mail function never fails. Is there something I have missed?

Code:

<?php

if (mail('', 'mySubject', 'myMessage')) {
    echo "Success!";

} else {
    echo 'Failure!';
}

?>

The output is "Success!"

Only by removing the argument entirely gets the else statement to execute. Does the to parameter need to be of a certain string format like the documentation states and if not, then how can I get this function to fail? Thanks

CodePudding user response:

The mail() function does not validate the input. It more or less just takes the data and hands it over to the system mailer daemon.

If that handoff was successful, the method returns true.

It it likely that your local mailer daemon will log an error that it couldn't process the email

CodePudding user response:

php mail() function is just a middle-man between your mail daemon and your code.
You need to manually validate email addresses, typically using something like

if( filter_var( $email_address ,FILTER_VALIDATE_EMAIL ) )
{
    mail(parameters);
}
else {
// handle error
}

Like @skaveRat said, you'll most probably find something in your email daemon log that this mail was not sent.

  •  Tags:  
  • php
  • Related