Skip to content
Advertisement

PHP mail successful with empty to parameter

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

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement