Receiving email in a phoenix 1.6 app -- should I switch out Swoosh and use Mailman?

I would like to write an elixir/phoenix application that simply serves as an email router – I want it to be able to receive emails, examine who they are from, and then forward them on to whoever they are supposed to go to based on a lookup table I will maintain.

In addition, I don’t want to have to pay for an external service such as SendGrid or some other third-party email solution.

I guess that gen_smtp is what people will advise me to reach for. Since I want to work in Elixir, it looks like I should try Mailman, since it advertises itself as an Elixir wrapper around gen_smtp.

So I’m curious whether I can use Phoenix 1.6 but swap out Swoosh for Mailman. Also, any advice about how I should go about this? Mailman doesn’t really have any docs to speak of. Or should I just try and stick with Swoosh, since I see that it also pulls in gen_smtp in it’s own mix.exs file?

Swoosh supports smtp transport too; I use it every day.

For what you are trying to do though, I think popular SMTP servers like exim4 already provides sophisticated rule based forwarding functionality. Polling the mailbox then forward the mail seems like a lot of work; plus I don’t think Swoosh can forward mails in a totally loss-less way. I am not sure about mailman but I would think any time you parsed the mail and re-serialize it out there will be some lost info and additional artifacts. In contrast, exim4 forwarding treats the mail blob as is.

1 Like

@derek-zhou thanks – this seems like valuable advice.

But why would I have to poll the mailbox? It seems to me that if I have gen_smtp running, then I can just write rules that will react whenever an email is received.

As for parsing the email, the only information I’m interested in is the FROM and TO fields – those will serve as the keys to determine where the email should be forwarded. I would prefer to leave the rest of the email untouched.

I’m curious why you think polling would be involved here. Just as a web server just sits around waiting and only responds when it receives a request, shouldn’t I be able to have an SMTP server with gen_smtp that just sits around waiting, but reacts instantly as soon as an email is received?

I am not sure gen_smtp can act as a full fledged smtp server; there are a lot of nuances in real world smtp. In any event the full smtp functionalities are not exposed via swoosh.

What if one uses this library?

Can this do the job if not swoosh?

Maybe; I have only used the client part of the library.