How to improve email robustness? [mailgun, laravel] - laravel

I planned to improve email robustness using below way. I am using laravel and mailgun. If any bounces email is in the mailgun then i want to send that to someone related to the email(not to that previous receiver and it related with previous sender or someone) so that i plan to use Task scheduler and schedule every hour this process. After that email send successfully i want to remove that email from mailgun.
I actually want to know how to implement get bounces email from mailgun and if send successfully using Task scheduler i want to remove that email from mailgun. if any sample's to achieve this? or any different ideas?

Normally when a bounce happens, providers such as Mailgun will retry a few times depending on the bounce status/type. After failing to send to that email address, Mailgun will put that address into your Bounces list to avoid sending to that address in the future. There's really no point in trying to send emails to an address which doesn't exist, inbox is full, domain expired, etc. So perhaps you should review your bounce list before you consider doing this. Mailgun's dashboard provides the description and date it happened. If you wish to send to a particular address again, you can remove the address from the bounce list. More about bounces here.
You can use mailgun-php or simply use Guzzle to call their API to retrieve your bounces. I'm not sure what you meant my removing email, but hope this helps.
Edit:
You can use their API to get all your bounces, and also the message content (retention depends on your plan or settings) if you need the original content. You could also use their webhooks to get the failed emails, so you don't really need to schedule/cron.
Edit 2:
The bounce list only stores email addresses of previously bounced emails. Also, I just realized that, if you're checking hourly for bounced messages, looks like the suppression/bounce API won't be of much help since you can't know if a message has failed for an address already on the list. This is the only data from bounce list:
{
"address": "alice#example.com",
"code": "550",
"error": "No such mailbox",
"created_at": "Fri, 21 Oct 2011 11:02:55 GMT"
},
So looks like you've to use the events API or webhooks to get the failed messages.
I've used mailgun-php sometime ago, and I found that the it wasn't easy working with Events on that library due to protected properties of the class. So if you have trouble, maybe just use Guzzle or use webhook approach instead.

Related

Can i put hidden token inside email while sending email through laravel

I want to send a email to the user from my laravel application. But i want to add a hidden token in that email so that user cannot see it and when user reply to that mail i can read that token and further use it.
Is it even possible?
I am using laravel 5.6 and sending emails using SMTP
There are several ways to do it but none is ideal. Let's start with your idea:
You can add a token within the message, but it can never be invisible, the contents of the message have to be fully visible. Even so, if the reply simply doesn't contain the original message, you will lose the reference
The standard way of tracking replies is based on Message-ID, since every email has to have unique Message-ID. Since you mentioned that you are using SMTP, that allows you to setup Message-ID upfront, otherwise, you'd retrieve it after the message is sent. It helps in a way that you can store that value in the database and automatically link it to the recipient.
Then, you'd look for In-Reply-To: and/or References: headers which should mention the original Message-ID if the reply is properly formatted, which doesn't have to be the case.
Ultimately, depending on your use case, you might not need any of these and you could just keep the sent email based on the recipient email address and extra information that you wanted to pass in the email within your database. Assuming that you'd expect an email from the same recipient email address, you could just figure out the token locally.

gmail API Users.messages: list is not getting replies

If I make a request to the gmail API for Users.messages.list it will return 100 of the newest messages.
If I make another request, 1 minute later and there are no new emails it will return the same 100 most recent messages.
If I make a third request, 2 minutes after the original and there is a reply to one of the 100 most recent messages it will still return the 100 most recent messages.
The problem with this is the it only returns the message id and thread id, not if there is a new reply or not. That would mean that I would have to check every message that I have locally stored, or every one of the 100 returned messages just to know if there was a reply to it or not.
The way that works, you couldn't "check" your email via the api because if you had stored 10000 messages and you were checking replies on all of them you would use up your entire API "number of requests" allocation in a single day!
What's wrong with you Google?
Sure I could use pop3 or imap but why when I could just use something like /list_recent?
You should definitely take a look at this part of the Gmail API documentation: https://developers.google.com/gmail/api/guides/push
Basically, it explains you how you can setup a "watch" over a Gmail account and receive notifications (new e-mail, deleted e-mail, labels added...) through Pub/Sub, the messaging queue protocol from Google.
The notifications will contain an historyId, which is a kind of milestone in the Gmail account. Using the /history endpoint, you'll then get the e-mails that were added and/or deleted since this history point. You have to store the latest historyId you handled somewhere in your app, so that you can query the right changes (and not miss anything) on the next notification.
If you don't need to react to changes in real-time, maybe you can call the /history endpoint periodically, but it will definitely be less efficient at scale.
It takes a bit of work to get this working but at the end of the day you get a very efficient system able to react about changes in real-time.

Not delivering to previously bounced address - Mailgun

I'm setting up my Laravel website, and now comes the part where I configure Mailgun to send and receive emails.
I followed different guides to do that, yet I don't feel I'm doing it right. I'm now stuck with this error:
Failed: postmaster#syrianafood.dk → ibrahim_hasan_eng#hotmail.com 'New Order from Ibrahim Hasan' Not delivering to previously bounced address
i.e. Mailgun is not able to deliver my emails. Could you please guide me through this? Thank you!
If mail was sent and rejected by the provider (bad content, headers, etc), Mailgun will automatically blacklist that address from being sent to in the future.
This is to prevent blacklisting yourself from many of the different MX providers out there.
If you think you've solved that problem, you can perform a DELETE request through the Mailgun API in the format of DELETE /<domain>/bounces/<address> before sending the mail, and then you'll be able to send to that address again.
You can check if a bounce has happened previously by performing a POST request to POST /<domain>/bounces. Furthermore, you will receive a JSON object back with a REASON as to why the bounce occurred, giving you the ability to respond in kind to this.
If a domain is not working correctly and it's not in the blacklist, then it's possible that the MX Provider its self is not accepting the emails and is responding unfavorably in a way that Mailgun cannot handle.
You can also delete the mail directly from the Mailgun UI if you have the login credentials. Please see the comment below and give it an upvote.

How to know mail send or not send in laravel and which recipient is not getting it?

Hi I am using the bellow function, can you please guide me how can I use Mail::failures(); in it
Mail::send('emails.caregiversetprimary', $templateArray, function($message)use($email)
{
$message->to($email, 'username')->subject('my subject');
});
Mail::failures(); < ====== this gives me black array as I have used wrong email
I am using laravel 4.1
tl;dr
You can know which recipients the email has been sent to but no which recipients have received it.
details
You need a better understanding of how mail servers work. From a Laravel point of view, there is no way (or at least not a simple one*) of knowing which recipients got the email. Its a matter of how mail protocol works. You may know which recipients the message has been sent to but no which recipients actually got it.
With Mail::failures() you get a list of recipients to which Laravel tried to send the email but it failed on actually sending it. But again, if it has been sent there is no straightforward way to known if the mail reached their inbox or no.
*If you happend to use Mailgun, Mandrill or any other 3rd party software then you are not dealing with a mail server 'per se' but with an API service. May be you could check the mail service provider documentation to research if they perform any kind of delivery tracking that you can programatically check.

How do I verify an email address is real and in use using the Sender Policy Framework

From what I've been reading the SPF can be used to validate email addresses by sending commands (rather than an actual email) such as HELO. I've managed to pick up a basic grasp of the policy but I can't get my head around how I'd go about solving the following problem:
I've got a number of email addresses attached to contacts in a CRM system and I'd like to find out if the email addresses are valid and still in use.
Currently we're using a REST Web Service (http://emailinspector.co.uk/) which returns "Ok" (if its ok... duh), "Bad" (if its not valid or not in use) or "Unknown". For Unknown, you are also provided some notes on why it came back with that, i.e. you are told if the Mailbox is full or if its a well known DEA.
I'd like to be able to program a script that can replicate this functionality and from what I've worked out it should use the Sender Policy Framework to do this? The problem is I don't know how I'd go about returning such precise information for "Unknown" email addresses.
Ideas and thoughts?
Actually SPF is just a text record, with some "hints" to let you know if an IP address or mail server is "allowed/Authorized" to send email for that domain. It doesn't tell you anything about an individual email address in that domain..
for example
[doon#qix:~] host -t txt labratsoftware.com
labratsoftware.com descriptive text "v=spf1 a -all"
The SPF record for one of my domains says that only the a record for the domain is authorized to send email for labratsoftware.com, and that if it doesn't come from that IP then it should be rejected (-all).
So the best you can do with SPF is tell that a received email came from an authorized host, and then use that information to help decide if you want to reject it or not.
The best way To test the validity of an email address you have, is to email it, and see if it bounces. You can use options like VERP (http://en.wikipedia.org/wiki/Variable_envelope_return_path) to automate the bounce handling. You can also try and connect to the MX records listed for the domain and try to deliver a message that way. Some Mail servers support verify (But most admins disable this to prevent information leakage). You can use RCPT TO to see if the server accepts it, but even if it does , you have no way of knowing if it will actually make it to their INBOX. My guess is that is what the API you are currently using is doing. And unknown are just ones that either don't answer, greylist, etc.

Resources