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!

Bloody bloody computers (TODO #1 update)

This is the update to TODO #1 – pointing my old monogreen.de domain to this blog (which is the hosted “free” plan from wordpress.com, i.e. no bells and whistles).

A bit of history: I’ve been using hosteurope to host my mailboxes and to register some domain names for years (I get a warm and fuzzy feeling by actually owning my email address and my mails – I’m hoping of course that hosteurope, compared to say, Google, is not interested in scanning my emails and selling my habits…). But I only bought the “email” package from hosteurope, and not anything else like servers or webspace hosting.

Naturally I’d like to point monogreen.de to this blog. As I’ve written last time, you have to have a webspace package booked in order to set that up, so I bought the cheapest plan.

First hurdle: It turns out they make that available quite fast (<15min) after ordering – but you have to log out and back in to see the new package in KIS. Groan.

Next hurdle: As I now had two packages (one email-only, one webspace which also includes a mailbox as well as webspace, php hosting, etc.), I had to “transfer” the domain monogreen.de from the email package. With some trepidation, I did so, as it seemed like the “redirect” setting would only be available on the webspace package, and I assumed I could still have the email addresses themselves point to the actual inbox I’ve been using for years.

Well that worked alright, and could immediately set up the redirect under the webspace package. However…the redirect didn’t work. Knowing things sometimes take a while to start working at hosteurope, I waited a while, but still nothing. Then, being suspicious, I sent myself an email from a different address/provider, and – it bounced o.O . I had managed to break my primary email address.

Photo Credit: Kalle Gustafsson https://flic.kr/p/GweE1X

Half a panicky hour later I had everything back to as it was before (including setting back up the various email aliases to the actual mailbox, which the “move” had severed) – and here’s two tips for anyone in this situation, and for myself in the future:

  • Every package in KIS has its own block of menu entries – and especially the “Domain settings”. Also there is a general one. So in my case there were three places to look for when trying to move the domain back from the webspace package to the email package (the correct one is the “general” area)
  • Many changes in KIS take ~15min to take effect (they say that in the interface, actually, but you usually assume this is only a general “yeah yeah mostly immediately but let’s cover our asses” policy – it’s not with hosteurope. Needless to say that this makes trying out things extremely cumbersome and error-prone…

And fun fact: After all that, the damn redirect still didn’t work…until next morning, after I had given up, and it suddenly worked just perfectly. Groan.

In summary: Mission accomplished but with too much panicky clicking around…

TODO #1 – my own damn domain

TODO for next time (update: here it is) : Make monogreen.de show this blog. I own the domain, I’ve registered it at hosteurope.com a long time ago – but it appears you can’t add any sort of redirect etc. except editing the DNS settings, and it’s probably quite useless to have monogreen.de point to WordPress’s server(s). After getting lost for many a year in KIS, the ever-so-confusing admin interface from hosteurope, I ordered the smallest “Web Pack”, which hopefully then lets me add redirects…you also get classic shared managed hosting – maybe I’ll also play around with that. Should be strange after years of administrating proper servers from scratch…