Why Rails/ActionMailer needs sendmail to send emails? Why not directly from Ruby?

For most Rails projects you are going to use Sendgrid, Postmark, AWS SES, etc. I also have knowledge about IP reputation, warming, SPF, DKIM, DMARC, etc.

However you are always hostage of some cloud provider to deliver your emails.

What if you want to use only on your own infrastructure/servers to send the emails (e.g. because you have large volumes)?

I see that the only option for sending from your own server in ActionMailer is using sendmail: basically Ruby will invoke an external command for each email.

Why is that necessary? Why not send directly from Ruby code (e.g. connecting to the SMTP server of the recipient in a background job)?

ActionMailer will connect to any SMTP server you like. The configuration options from the guide outline how to do that.

By default, email send via ActionMailer does use a background job, if you have a background job library installed.

So, it should do exactly what you want already.

I was wondering why delegate the delivery to sendmail instead of using Ruby SMTP directly.

ActionMailer will connect to any SMTP server you like

This is for your mail server, not to deliver to the end user email over SMTP.

The question was more about having the mail server built-in in the rails application. I found this for example: GitHub - postalserver/postal: ✉️ A fully featured open source mail delivery platform for incoming & outgoing e-mail

Hi Marco, integrating a Mailserver into the default rails package, i would see as a overkill. If something like Postal would be available as a gem, that you could optionally plug-in could be a nice idea for apps that should be able for a fully-mail support including receiving emails. But such a gem would need heavy maintenance. May the day come if the demand is big enough :slight_smile:

You say, you know about SPF, DKIM, DMARC etc. probably reverse DNS lookup?

Then you know how hard it is to convince a modern mail server, to accept incoming mail. You normally have some mail service (you need a valid sender address) so my advice: use a authenticated account at this service to send your mails. Otherwise most mail server, especially big ones like Google, GMX will reject your mails.

@mameier I know that the easiest solution is to use a service like Sendgrid, Postmark, AWS SES, etc. and I do that all the time.

However my question here is more about if it’s possible to be independent from these cloud providers. I would be happy to hear from people that made it and are sending from their own servers.

Ah, of cause! I never use cloud providers but my own mail servers running either Postfix (SMTP) and Dovecot (IMAP) or using Zimbra.

I prefer Postfix/Dovecot some of my customers prefer Zimbra.

Postfix/Dovecot is some work to fulfil all the conditions for modern mail servers.

your could start with mailcow to setup an own mail server in a docker image.

The issue is not Rails but running a widely accepted mail server (accepted by other mail servers).

I find myself in a situation where I need to send emails exclusively from my own infrastructure and servers. This is primarily because of the high email volume my application generates, and I want full control over the email delivery process. I’ve noticed that the default option in Rails’ ActionMailer often involves invoking an external command like sendmail . However, I’m curious about the possibility of sending emails directly from my Ruby code by connecting to the SMTP server of the recipient.

1 Like

One possible reason why this particular option hasn’t been made easier is that it would consume one of your processes until the message handshake was complete. E-mail is a store-and-forward proposition, going all the way back to the original intent of the ARPANET. There is no expectation that the recipient is available or there is even a route to their machine. Any sender has to be prepared to cache outgoing messages until the send succeeds (or some timer or attempts counter is exceeded). Sendmail (and anything that quacks like it) provides all that nuance and patience.

There are many other possible reasons, but that’s the one that leapt to mind…

Walter

Sendmail is a MTA. At the most basic level a MTA connects to the recipient SMTP to deliver the message. But there is more to it than that. Since it may be offline it has a retry mechanism, queues to control the rate of mail flow, etc.

Could you implement a MTA in Ruby? Sure, but that’s really outside the scope of Rails. Rails’ job is to hand it off to a MTA to send it out. Since there are so many good MTAs out there (qmail is my preferred) it seems better to just set that up rather than write a whole MTA in Ruby.