Search code examples
yii2swiftmailer

How can I log failed mails into database using swiftmailer


How can I check failed mails. Swiftmailer is returning true even if i supply invalid email address. I need to log send and failed mails into database

$message = Yii::$app->mailer->compose();
$message->attach($protocal.$_SERVER['SERVER_NAME'].$payslip);
$message->setTo("tft@sjkdsjk.dfdh");
$message->setFrom("hr@gmail.com");
$message->setSubject($vStaffName." ".$vPeriodName. " Payslip");
$message->setTextBody("Find attached copy of your payslip. To open this document use your ID number as password");

$send = $message->send();

var_dump($send);die(); //returns true always

I would expect true or false


Solution

  • You should check with SMTP.

    That means you have to connect to that email's SMTP server.

    After connecting to the SMTP server you should send these commands:

    HELO somehostname.com
    MAIL FROM: <no-reply@gmail.com>
    RCPT TO: <emailtovalidate@domain.com>
    

    If you get " Relay access denied" that means this email is Invalid.

    There is a simple PHP class. You can use it:

    http://www.phpclasses.org/package/6650-PHP-Check-if-an-e-mail-is-valid-using-SMTP.html