Currently we are using Mandrill to send our emails, when ever Mandrill detects a bounce we get the original headers along with the bounce or a subaccount where the email was send from.
In Amazon SES we are getting the notifications through SNS thats no problem but besides the email from the user we get no original information back. So we have no idea what email campaign the user bounced on etc.
Anybody that knows how to handle this?
every SNS message contain a "mail" part with the messageId see. If you are sending a mail you receive as the only response the messageId see.
so I store all the messageId together with information I later maybe need if that mail bounces or gets complaints and if I receive a bounce I can query all the information with the given messageId.
You can use the regular expression to get messageId
In python i use like this
message_id = re.findall(r"(\w+(?:-\w+)+)#email.amazonses.com", json.dumps(event))[0]
Similar thing you can try in php
Related
I have a PHP web app with a section to send invoice by email to all partners.
Each partner could have more than 1 email.
For example: COMPANY-1 with emails: email_1#company1.com and email_2#company1.com
I send all with phpmailer.
I save into my database the send-response from server mail ( in this case gmail).
If sent, i'll save true, else false - foreach email.
But some emails sometimes are incorrect (due to error typing when insert) or are dismissed by the company (in this case the company do not inform us and we still send to this deleted email) and I like to retrieve these "problems" using web app.
(If I login into google webmail I can see the email returned for each sent to an not existing email, with some advice/error code inside... but i don't know how to retrieve using PHP)
I tried using
$inbox = imap_open($hostname,$username,$password);
$emails = imap_search($inbox,'ALL');
This script works, but using ALL i get timeout.
I don't know the best method to retrieve these email of "NOT DELIVERED EMAILS FOR SOME PROBLEMS".
My first ideas was to set a custom Message-ID in the header when I send:
$messageID = "<" . md5($_SERVER['HTTP_HOST'].(idate("U")-1000000000).uniqid()).'-'.idate("U") . '#' . $_SERVER['HTTP_HOST'] . '>';
$mail->MessageID = $messageID;
and then in my script, using a command like:
$messageID = retrieve_from_database(...);
$emails = imap_search($inbox,'HEADER ' . $messageID);
but it doesn't exist a query HEADER
I'd like in my web app to show, for each email sent, eventually problem of bounced or undelivered but I don't know how and what I need to "ask" to imap_search to retrieve responses associated to the original sent email.
I read criterias list from docs, but I don't find anything usefull I think: https://www.php.net/manual/en/function.imap-search.php
Example of a scenario I need:
I send an email with Message-ID: <test#testXYZ> from my_email#mywebapp.com to email_not_existing#test.com. The server response true cause sent is done.
I insert a new entry on my database table emails_sent with send result true: INSERT INTO emails_sent (ID, email_dest, sent, MessageID, when) VALUES (455, 'email_not_existing#test.com', TRUE, $messageID, '2019-05-30 8:00:00')
The gmail daemon reply to my_email#mywebapp.com with a message of undelivered email
In my web app, when I go into page of sent emails, foreach ID from emails_sent table, i need to search on imap eventually response email with errors. In this example case when I ask for ID 455, I need to search on imap_search messages replyes associated with my email sent with MessageID assigned by me (or other method i don't know....)
I would advise you to get away from shared hosting and get a proper server (try scaleway.com). As you're discovering, it means you don't get sufficient control.
I'd recommend against using IMAP as your database - as you're finding it's not good enough for the kind of searching you want to do. If you import all the bounced messages into your own DB, you're free to parse and search in whatever way you like, and it's also easy to get all messages from IMAP.
The most efficient way to handle bounces is to get your SMTP server to pipe bounce messages directly into a script. This will give you the complete bounced message, along with environment variables that tell you more about the delivery. That of course requires that you have config-level access to your mail server, or a hosting provider with a clue.
Be warned that bounce handling is an exceedingly unpleasant task, and is very difficult to do well. You may want to look at a 3rd-party bounce handler like Boogie Tools bounce studio to save you some sanity! Aside from some very broad top-level standards, there is very little consistency at a lower level, and bounces are often useless. For example, MS Exchange can produce bounces that do not contain the bounced message's original target address at all! The way around this is to use VERP, where every address (or even every message) gets its own unique return path, allowing you to know where a message bounced from even if the message content contains no clues.
I am using AWS SES with PHP SDK to send emails with attachments to customers, some of them provide an invalid email causing SES to complaint or bounce. I would like to find out what emails have failed and do a follow up by inactivating that item, calling the customer to provide a valid email or any other solution.
Is it posible to get bounce or complaints in some way?
Yes. But you really should be validating email addresses before you ever attempt to do anything with it.
You create an SNS topic, and add one (or more) subscriptions. There are a number of different options when creating a subscription, but you probably want to use https. You can have multiple subscriptions, so put your email address in there too so you can see what information is coming in.
Then, in SES, set up your SNS topic to handle bounces and/or complaints.
The AWS PHP toolkit has code and examples to handle bounces & complaints - it's pretty simple. You'll also need to implement a validation method - SNS won't start sending data to your endpoint until it has been validated.
I am trying to send mail using Mandrill API and I have got following responses :
[{"email":"_emailid_","status":"queued","_id":"772237d78ce74e5c9bd8ccbd9a1cfb8c"}]
{"email":"_emailid_","status":"scheduled","_id":"10a0b596a95e44ddb5e8cf504e3899ce","reject_reason":null},
{"email":"_emailid_","status":"scheduled","_id":"b699d58f021b43b2a0b25d801d1b4f91","reject_reason":null}
But I am not getting any mail on the email addresses sent. Please help.
Do I need to whitelist the IP of the system from where I am doing the curl call ?
You do not need to whitelist any ip addresses, you however do have to configure the DNS records for your email domain properly, head over to the domain settings page in the mandrill control panel for more information.
Everything seems fine.Either your you are not providing correct email to api or mail are coming to your spam folder.You need to check mail from address which you give to the api.
It looks like at least the last two were scheduled, which means they won't send immediately, but rather at the scheduled time in the future. You'll probably want to double check the time and date that they were scheduled to be sure.
For the first one, there are a number of reasons that emails are queued, including if they have attachments that need to be scanned, or if they're part of a large/batch API call. We'd need more information about the API parameters that you passed to determine the reason for it being queued and/or what happened once it was queued.
You can search your Outbound Activity by the recipient email address, subject line, sender, and a number of other criteria for emails that have been sent. Messages that are scheduled but haven't sent yet cannot be searched since they've not been processed. For any messages that have sent, click on the word "Delivered" on the Outbound Activity page to see more details about the response that Mandrill got from the receiving server when delivering the message.
I would like to know what's the best way to manage answers to email notifications.
eg. The user receive an email notification, and instead of having to click on a link to go on the website and answer it he could directly reply to the email.
There's no best way. Depending on what you want to do with the responses, you could as well handle them with thunderbirds message filters ;) .
Put a unique identifier into the subject or the body of the notification that you're sending so that you can identify & classify the email correctly when a user replies by email.
If you want to stick with php: pick up the php pop3 client class from: http://www.phpclasses.org/browse/package/2.html or find a client elsewhere and then just parse your message responses and classify / handle them however you want to.
Use the Message-ID header field to uniquely identify each message. If there is a reply on that message, the client should put that message ID in the In-Reply-To header field in the reply message. See RFC 2822 for further details.
Stick an id and authentication token in the subject line.
Pipe it into a script that can process it with Procmail
As a learning exercise you could read the php code and Documentation to the drupal module http://drupal.org/project/mailhandler mail handler. This accepts emails and looks for script like instructions.
The answer above about checking the message id is also very valid.
I am building a symfony module for sending newsletters. the idea is to build a queue list of adreeses to receice the mail and then ,with a cron job, send let's say 50 at a time.
What i don't now how to do is to read the Mail Delivery reports that are sent back by the server when an email adress doesn't exist or the mail is full. The idea is to store these error reports an clean the adress list.
Any ideea how to implement that?
When reading the "bounced inbox", you can use a class like this to actually parse the mail and see what status was returned (e.g. permanent or temporary error):
http://www.phpclasses.org/browse/package/2691.html
To really parse a mail accurately will give you a hard time, as not all mailservers are alike and some will send you a "mailbox full"-error marked with a "permanent" flag while others may tell you that the error "user doesn't exist" is "temporary".
I tried a solution for this once and ended up setting up my own parser connected to a huge database containing possible server replies (and their "real" meaning :).
You can use a reply to address while sending. So bouned emails will be sent to this id. You can also create another PHP script which will read this "reply to" email inbox and get the id from it. You can then remove this id from the list you have.