Form to Email PHP Validation fallback using PHPMailer

两盒软妹~` 提交于 2019-12-04 22:24:27

tl;dr: both statements evaluate to true. It's better to return true or false instead of strings and handle the message later.

First I'll take care of your question, then I'll make some suggestions on good practices.

When you use return x; in PHP and most languages, you're "sending" x back to where you called the function. So, when your code is executed it will be read as:

if('ok')

or

if ('Error info...')

PHP evaluates the condition on an if statement (this is the part between parenthesis) as true or false by converting it to the boolean type. The string to boolean conversion in PHP is basically as follows: any non-empty string evaluates as TRUE (follow the link, check first table, last column).

So, your function is returning 'ok' if it succeeds, 'Error info...' if it fails, these are both non-empty strings and thereof evaluated as true, so no matter if the first email sending attempt went well, your script will try to send the second one, and will always set $msg to 'Email sent!'.

Here's some advice on how to fix your script so it works (and looks) better:

  1. As @Matt suggested it's always best to validate the data by yourself instead of relying on PHPMailer to do so. Despite PHPMailer will return an error if the destination address is invalid, it's a good practice not to even call the library if the email is not valid. So:

    • First, validate the data using javascript, so your user get's instant feedback.
    • Then, validate it using PHP (maybe create a new validate() function that may use filter_var() to validate emails.
    • Last, send the email only if the previous two were successful.
  2. To follow your chain of thought, you should be evaluating if the string returned by sendemail() equals to 'ok' or not:

    if (sendemail(...) == 'ok')
    

    But, instead of evaluating two different strings ('ok' or 'Error info...') it's better if the function returned boolean values instead, and since PHPMailer's send() already does, just keep it as you have it commented:

    return $mail->send()
    
  3. Your last line is using $mail, a variable that you declared inside a function and you never made global, so it won't be available at that point and since you're trying to get a property (ErrorInfo) you'll be firing two PHP notices: Undefined variable and Trying to get a property from a non-object. You COULD just add global $mail at the top of the function and that will make it globally available (outside your function's scope) but this is considered a bad practice since in large pieces of code you might get confused.

    Instead, a neater way of firing the error would be to throw/catch an exception:

    function sendemail(...) {
    
        // ... PHPMailer config ...
    
        if ($mail->send()) {
            return true;
        } else {
            throw Exception('Error: ' + $mail->ErrorInfo);
        }
    }
    
    // later...
    try {
        sendemail()
        $msg = 'Email sent!';
    } catch (Exception $e) {
        $msg = 'Email failed!' . $e->getMessage();
    }
    

    Here, if there's a problem with the emails sending, your function will throw a generic exception and the catch part will be executed.

    EVEN BETTER

    If you initialize PHPMailer like this:

    $mail = new PHPMailer(true); // note the parameter set to true.
    

    It will throw an exception by itself if it fails to send the email and you'll be able to catch the exception:

    function sendemail(...) {
        $mail = PHPMailer(true); // this line
        // ... PHPMailer config ...
        return $mail->send(); // just to return something, we aren't really using this value anymore.
    }
    
    // later...
    try {
        sendemail(...)
        $msg = 'Email sent!';
    } catch (phpmailerException $e) {
        echo $e->errorMessage(); // Catch PHPMailer exceptions (email sending failure)
    } catch (Exception $e) {
        echo $e->getMessage(); // Boring error messages from anything else!
    }
    

Never forget to read the docs!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!