How to send mails from Rails via a Hosteurope SMTP server

‘Sup, babes!

Just noting down something that should be trivial but has taken me an embarrassingly long time recently. Let’s write it down here for the edification of the masses, so they may be entertained and enlightened, and for me to look it up again next time.

Hosteurope, if you have an account there, provides you with an SMTP mail server which you can use to send out emails. I’m using it from Thunderbird (yeah I’m old, sue me), but you can also use the server programmatically to send out mails (I guess as long as you’re not overdoing it – this is probably not a good idea if you’re about to send out a large volume of mail, but for a hobby project it’s much simpler than setting up a dedicated mail service account like Mailgun and friends).

So yeah the principle is of course quite easy – just follow the example in the Rails guides.

Except it wasn’t for me, I had to try out a million combinations of all those options – most don’t actually change anything, it turns out. It wasn’t helpful either that the error message for most combinations was – a long hangup and then a timeout 😦

So here’s what works for me:

config.action_mailer.smtp_settings = {
  address: "<hosteurope SMTP server address for your account>",
  port: 465,
  user_name: "<username>",
  password: "<password>",
  authentication: :plain,
  enable_starttls_auto: true,
  tls: true,
}

So yay this is using TLS, and is otherwise super simple. If you don’t want to be like me and waste additional time of your life by using the wrong email server you’ve saved irresponsibly, here’s how you can get the mail server info from the confusing hellish vortex of unusability that is KIS:

  • Log in at https://kis.hosteurope.de/
  • Go to:
  • “Product admin”
  • “Domain and Mail”
  • “E-Mail”
  • “Manage e-mail accounts / Autoresponder / Filter / Webmailer”
  • Find the row with the relevant Email account (you likely only have one)
  • Click on the little (i) i-in-a-circle “Account information” button
  • Phew

What you want is under “Outbox”.

Sending mails locally without copy-paste shenanigans

So that’s that. Here’s another tip to make life easier while trying this out: Put the config block above not in config/environments/production.rb but in config/application.rb (which is shard between all enviroments), and add this around it:

if Rails.env.production? or (Rails.env.development? and ENV["SEND_REAL_MAILS_IN_DEVELOPMENT"] == "true")
  config.action_mailer.smtp_settings = {
    ... as above
  }
end

Then you can use the ENV switch to quickly try out sending from your local setup. For example, if you’re using Devise, you can abuse an existing local User to send yourself a mail at “yourown@email.com”:

$ SEND_REAL_MAILS_IN_DEVELOPMENT=true rails c
2.6.5 :001 >u = User.last; u.email = "yourown@email.com"; u.send_confirmation_instructions

Extra bonus tip because I love the “new” Rails encrypted credentials so much

So you probably shouldn’t check in your actual SMTP credentials to source control. Instead, use the neat encrypted storage vault of awesomeness:

if Rails.env.production? or (Rails.env.development? and ENV["SEND_REAL_MAILS_IN_DEVELOPMENT"] == "true")
  config.action_mailer.smtp_settings = {
    address: Rails.application.credentials.hosteurope[:smtp][:server],
    port: 465,
    user_name: Rails.application.credentials.hosteurope[:smtp][:user_name],
    password: Rails.application.credentials.hosteurope[:smtp][:password],
    authentication: :plain,
    enable_starttls_auto: true,
    tls: true,
  }
end

And then, on the command line:

$ EDITOR=micro rails credentials:edit

And add the actual credentials there:

hosteurope:
  smtp:
    server: "foo"
    user_name: "faa"
    password: "fii"

And now you can check it all in. Here’s a good article on the feature. Oh, and shoutout to my preferred terminal editor: https://micro-editor.github.io – because nano sucks and vim and emacs are for aliens. What an efficient way to offend many people all at once 😉

That’s that, folks, see ya next time!