Search code examples
haskellsmtp

How can I tell if an email bounced?


I'm using the smtp-mail library to send emails from my application with the sendMailWithLogin' function.

I would like to know if the email was successfully delivered or if it bounced. And if it bounced, with what error code? All of the functions return IO () and digging through the code it's a lot of layers of IO (). Is there some way to get this information with this library? Or is there perhaps some other library that can be used?


Solution

  • Unfortunately your software will have to become significantly more complex to support this. If you want to discover a bounce notification, you will at minimum need to be able to receive mail, which is not the purview of SMTP; you will need to make a POP or IMAP client. Also keep in mind that

    1. I'm pretty sure there's no obligation for an SMTP server to notify you of a bounce.
    2. A failed sending is retried, often for several days, before giving up.
    3. There is, as far as I know, no standard mechanism for bounce notification, so you will be solving a somewhat difficult problem. Getting coverage of all the formats used by all the world's email servers likely has a very long tail; perhaps a more modern version of this would be using some AI (e.g. an LLM) or statistical model (e.g. one of the standardish spam filtering techniques, but retooled for detecting bounces instead of spam) to try to actually understand the text inside emails you get back.

    Carl mentions that a MAIL FROM:<> header usually indicates a bounce -- it may be that noticing this gets you most of the benefit with only a little extra work.