Search code examples
phpemailphpmailer

PHPMailer does not give error message?


Im creating a little Email Script at the Moment with PHPMailer + SMTP Authentication. I tried now sending an E-Mail using a wrong Passwort - but it still gives back "true" for success... anyone have any idea?`

Here is My Function, that i use to call sendmail:

$erfolg_email = true;
    foreach($empfaenger as $value)
    {

        $response = $this->sendMail($smtp, $value, $content, $files);
        if($response != true)
        {

            return $response;
            $erfolg_email = false;

        }

    }

And here is my PHPMailer Function

function sendMail($smtp, $empfaenger, $content, $attachements)
{

    $mail = new PHPMailer(true);

    try{

        $mail->IsSMTP();    
        $mail->Host = $smtp['SMTP'];
        $mail->SMTPAuth = true;     // turn on SMTP authentication
        $mail->Port = $smtp['Port'];
        $mail->SMTPSecure = 'tls';
        if($smtp['Domain'] != '')
        {

            $username = $smtp['Username']."@".$smtp['Domain'];

        }else
        {

            $username = $smtp['Username'];

        }
        $mail->Username = $username;  // SMTP username
        $mail->Password = $smtp['Password']; // SMTP password
        $mail->SMTPOptions = array(
            'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
            )
        );
        $mail->From = $smtp['Email_Address'];
        $mail->AddAddress($empfaenger);
        $mail->WordWrap = 50;
        $mail->isHTML(true);

        foreach($attachements as $value)
        {

            $mail->AddAttachment($value);

        }
        $mail->Subject = "Mahnung";
        $mail->Body = $content;
        $mail->AltBody = "Sie haben offene Posten";

        if($mail->Send() == true)
        {

            echo $mail->ErrorInfo;
            return $mail->ErrorInfo;

        }else
        {

            return $mail->ErrorInfo;

        }

    } catch (phpmailerException $e) {

        return $e->errorMessage(); //Pretty error messages from PHPMailer

    } catch (Exception $e) {

        return $e->getMessage(); //Boring error messages from anything else!

    }
}

$smtp contains an array with all the SMTP INformation, Email address, Signature, Smtp Server, Port, Username, Password and SSL Usage...

I am pretty sure, I am using the wrong username and password, as no Email is getting through - but i still get "true" as a result of the send mail function... its not even echoing the error. I did even try to give an Error Message, when sending is successfull... But nothing

Any help is apreciated!

Cheers


Solution

  • Your function sendMail doesn't return a boolean. Since it's an array / object the result is always true in this case. Try printing $response before your if statement and you will see the error.