Sagar Gautam
Sagar Gautam

Reputation: 9359

How to check mail is sent or not in laravel?

I'm currently working on laravel mail. I've sending email structure with attachment like this:

$document  = Document::find($request->document_id);

Mail::send('email.senddocs', array('project'=>$document->project->name,'type'=>$document->documentType->type), function($message) use($request,$document){

    $file_name = public_path().'/documents/'.$document->project->p_key.'/'.$document->file_name;

    $message->from('[email protected]', 'DDMS');
    $message->to($request->email);
    $message->attach($file_name);

});

I've already visited here. But, the process over there always returning success.

What I actually want is to know if mail is send or not. In my case, Mail sending can fail due to fake email like [email protected] or by some other errors occurred.

How can I know mail is sent or not ?

Any help is appreciated

Upvotes: 0

Views: 9777

Answers (1)

yfain
yfain

Reputation: 496

This questions is asked several times here: Laravel 5 - check if mail is send

You can use the failure method for this:

if( count(Mail::failures()) > 0 ) {

   foreach(Mail::failures as $email_address) {
       echo "$email_address <br />";
    }

} else {
    echo "Mail sent successfully!";
}

This only checks if a email was send. You can not handle not existing email adresses with this method. You can use mailgun for this problem for example.

Another way is to use a php class which connects to a smtp server, and will check.

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

Some Informationen of checking email adresses: How to check if an email address exists without sending an email?

Upvotes: 2

Related Questions