
Imagine that you have a web application and one of its functions is the mass mailing of news to your users.
For some reason, part of the email addresses of users are non-working or incorrectly filled. Would such users be automatically unsubscribed?
You can add a special header to the email messages you send.
Return-Path: <noreply@my-application.com>
Notifications from the mail server about failed deliveries will be sent to this address.
As a result, the whole task comes down to reading these notifications, retrieving the email address, and marking the user with this address as unavailable for distribution.
And now a real example.
In one of our applications, the mail server adds several useful lines directly to the body of such notifications:
')
Final-Recipient: rfc822; nonexistent@example.com Original-Recipient: rfc822;nonexistent@example.com Action: failed Status: 5.1.1
For ruby ​​there is an
IMAP client in the standard library. Connect to our “noreply@my-application.com” mailbox and read the notifications:
imap = Net::IMAP.new('mail.my-application.com', port: 993, ssl: true) imap.login("noreply@my-application.com", "mysecretpassword") imap.select("INBOX")
For clarity, you can still check the status code. The code 5.XX indicates a fatal delivery error, for example, the email address does not exist, the code 4.XX indicates a temporary error, for example, the receiving server does not respond.
This code is run periodically in the background using
Resque ,
Delayed :: Job , cron, or any other tool used in the application. Problem solved: mailing is sent only to working existing email addresses.
The task was prepared and solved together with
olemskoiPS: In your application, such notifications may not be so verbose, but you can still try to retrieve a non-working email. For example, if you use gmail to send emails, then notifications about a failed delivery from it will contain a specific header:
X-Failed-Recipients: nonexistent@example.com
which can be used.