Similarly to a rest api, I want a server to listen for an email to an address I have created, and in response to mail being received, run code that I have created. Is this possible already? I understand how to call an api or create a micro service, but it seems like triggering code when someone sends an email is esoteric.

In my research, it seems like I need an smtp server and pubsub data structure. However, in the implementations I have found on GitHub, it seems they are more tailored to sending email rather than triggering code when a user receives an email. I would prefer not to use sengrid or ses aws.

If smtp server is what I need, do you think it is something I can complete in a semester.

  • Your usage scenario description is pretty incomplete. Contents of an email which is send to - whom? One fixed email address of a company? All email adresses within a specific subdomain? And so on. Give us a bigger picture! – Doc Brown Jan 28 '20 at 7:18
  • If you are talking about running code when a member of the public receives an email from you, the answer is absolutely not, as that would be a security problem. Even retrieving an image is usually blocked because the http request can be used to track the end user. If you are talking about communication between two endpoints that you own, that is something else. – John Wu Jan 28 '20 at 8:28
  • @john wu The question was if the technology exists, not if it was wise to do so. – Martin K Jan 28 '20 at 9:57
  • Your question isn't clear. If alice@example.com sends a mail to bob@example.com, do you want the API to be called on Alice's machine, Bob's machine, or on the mail server? – Simon B Jan 28 '20 at 10:13
  • @MartinK My comment was not about whether it is wise but whether it is possible, i.e. if the technology exists. – John Wu Jan 28 '20 at 15:47

Common e-mail server software (postfix, sendmail, presumably exim but I didn't check that one) supports forwarding to programs via pipes, so this isn't esoteric at all.

Ticketing systems such as Redmine and mailing list managers such as GNU Mailman use it all the time.

If you prefer not to run your own e-mail server, you can programmatically access mailboxes at almost any provider using POP3 or IMAP protocols. This will cause some delays due to polling, but e-mail isn't supposed to be real-time anyway.

  • I have look into sendmail and postfix, and neither seem to be have documentation of outputting an email message to another script. Do you have a link to a section in their documentation or an example on GitHub? – Rahmi Pruitt Jan 28 '20 at 17:29
  • 1
    See the postfix manual pages on aliases (postfix.org/aliases.5.html) and the local delivery agent (postfix.org/local.8.html). Both describe ways of piping incoming mails to commands. – Hans-Martin Mosner Jan 28 '20 at 18:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.