0

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('us@example.com', '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 akdjbaksdjf@jhbasdhadfs.com or by some other errors occurred.

How can I know mail is sent or not ?

Any help is appreciated

Sagar Gautam
  • 9,049
  • 6
  • 53
  • 84

1 Answers1

2

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?

yfain
  • 509
  • 7
  • 23
  • @yfail Let me check – Sagar Gautam Aug 07 '17 at 07:39
  • I've added mail as `jhbdajfsd@jhbasdjf.com` but says **Mail sent successfully!** – Sagar Gautam Aug 07 '17 at 07:41
  • This also right. Cause Laravel don't check if the Mail exists. For things like that you have to use mailgun for example. You can only check if there is a problem sending an email or other things that may happened. – yfain Aug 07 '17 at 07:43
  • I've dumped the `count` of mail errors it shows **0** – Sagar Gautam Aug 07 '17 at 07:44
  • Yes, because there aren't any errors. I told you, that laravel not checks if a email adress exists. It only does normal validation things. For something like this, you should use mailgun for example. Here is a good tutorial to get in touch: https://laravel.com/docs/5.2/mail – yfain Aug 07 '17 at 07:45
  • Then, We can't check such things in laravel ? – Sagar Gautam Aug 07 '17 at 07:46
  • No. To check if a email adress exists, you have to send one. This email will be rejected and returned back to your mailserver. Then you have to check in your mail server. Mailgun does this for you. You can also do more things ;) – yfain Aug 07 '17 at 07:47
  • can you add some details about checking valid email address ? – Sagar Gautam Aug 07 '17 at 07:49
  • 1
    You can try this class: https://www.phpclasses.org/package/6650-PHP-Check-if-an-e-mail-is-valid-using-SMTP.html – yfain Aug 07 '17 at 07:51
  • Thanks for following up here and for your useful answer. – Sagar Gautam Aug 07 '17 at 07:52
  • 1
    Read this topic, cause there are some more information about verfied email adresses. I could be useful: https://stackoverflow.com/questions/565504/how-to-check-if-an-email-address-exists-without-sending-an-email – yfain Aug 07 '17 at 07:53
  • I've viewed that answer but i don't understand that. – Sagar Gautam Aug 07 '17 at 08:00
  • What exaclty you don't understand? – yfain Aug 07 '17 at 08:01
  • How to check RCPT for given email – Sagar Gautam Aug 07 '17 at 08:01
  • Here is a tutorial: https://ctrlq.org/code/20152-validate-email-address - But keep in mind that this is not a 100% way! – yfain Aug 07 '17 at 08:02