So You'd Like to Send Some Email (Through Code)

I have what I would charitably describe as a hate-hate relationship with email. I desperately try to avoid sending email, not just for myself, but also in the code I write.

This is a companion discussion topic for the original blog entry at:

Or just use an app that’s done most of that for you -

You should check out some of the cloud based email providers. They manage this headache for you. A good one is

+1 to both comments above. 3rd party SaaS is the way to go. Then, if pricing appears to be an issue, then you’re probably sending too many emails anyway.

The most important thing: make sure the SMTP server is not listed in one of the many DNS-based blacklists.

Also, you should have your SMTP server do the DKIM stuff. That way every email message (not just the ones sent by your application) will be signed.

That doesn’t seem that onerous.

Sure email is crap but all you are saying here is “oh noes I have to follow the standards involved”. At least there are fairly universal standards.

idno, maybe I’m missing something

I just configure exim on the server to use GMail as a smart host. Using my Google apps account I’m able to keep my domain on there, instead of I trust Google to do all the wacky SMTP stuff properly. It seems to work, because everyone gets the e-mails my apps send out.

Why go through all this complicated mess, which I’ll probably screw up, when I can just have someone else do it for me?

Also be sure not to use anything that could even resemble the language used in spam, down to the name of your company. - “After 90 years, one of Canada’s oldest magazines, The Beaver, is changing its name.”

Another nightmare is the state of HTML rendering of mails. can help you to check out how your mails looks in most of clients in use.

On a completely different note I wish there will be a more high level mail implementation included in the stdlib for Ruby in the future.

require 'mail' # <a href=""></a>

Mail.deliver do
     from ''
       to ''
  subject 'Send Some Email'
     body 'Thanks for a great post!'

Email is great, it is the best communication tool nowadays. It is also used as the primary way to log on to your blog so we can post comments :wink:

@Dejan, yes, you should put your public key there. See my post:
How to setup DKIM and DomainKeys with SmarterMail


Ironically, my e-mail server rejected port25’s automated e-mail because it was not sent from a valid address:

sender verify fail for <>:
  response to "RCPT TO:<>" from [] was:
  550 5.1.1 no such local recipient

Hmm, as Ovidiu suggests having your local SMTP server do this stuff for you… I’m pretty sure most individual email clients don’t have personalized DNS records with public/private keys. (But I’d love to see S/MIME get mainstream!)

I’d be more curious about how to get through authentication mechanisms on the SMTP server, and then let it do the heavy lifting. For my organization, it looks like that’s just a matter of piping everything through an SSL connection with a username and password.

Wow, is that you’re only beef with sending email? You didn’t touch on the nightmare of the various authentication methods, using SSL or setting up SMTP relay hubs. There’s the minor annoyance of attaching “HTML” email bits to a message, but that’s a minor barb.

Hell, even if you do this dance right, the mail will often get stuck in a spam trap or not get to the addressee for a long time due the way ISPs batch SMTP traffic.

And what if your ISP gets on some stupid spammer black list? You may not have done anything wrong, but some idiot on your domain did once and now you get to pay the price.

Seriously, I cringe every time I’m asked to add “email notification” to an app. It’s a disaster.

Ha, you’re a mind-reader Jeff - we were fighting with this very issue just this morning. Thanks for the post, it was just what we needed.

One thing I would add: if you’re sending significant amounts of important emails, make sure to check bounce-backs, because there are also issues of customers having mis-configured mail servers, bad email addresses, etc etc - there’s no substitute for a human checking these issues (sadly).


It’s good to see someone talking about this. This is one of the points I wanted to get around to once I got my blog set up.

Another few things that are important:

  1. Either VERPs or Queue ID headers so you know who you sent the email to when it bounces (and they /will/ bounce.) VERPs and Queue ID headers make life very simple for knocking out bouncing email.

  2. Sign up for feedback loops with the main providers (at least AOL, Hotmail and Yahoo).

  3. Add a list-unsubscribe header that has an email address that also routes through to your feedback loop email.

By the way, as a long time reader and first time commenter, thank you so much for all the work you put into your blog. I know from my own experience trying to maintain one that the motivation can be difficult. Hopefully I’ll get there soon.

Those are serious headaches. But I’ve always been mystified by your hatred for email. Can you propose a better system? It has to meet the following requirements

  1. Your address can be public, so anybody can send to you without having an explicit prior relationship. This opens the door for spammers, but also job applicants, customers, etc.
  2. A message can be private - readable only by recipients
  3. A message can have multiple, explicit recipients - not ‘whoever happens to read it’
  4. Receivers are notified when they receive messages and can respond to the sender, other recipients, etc
  5. Receivers are able to archive and search their sent and received messages
  6. Message storage can be in the cloud or on a local machine, whichever suits your purposes

I’m sure there are other advantages of email.

Yes, young people are using text messages and whatnot. But what’s good for ‘hey whos brylie dating today lol’ isn’t necessarily good for serious communications. And I bet those same kids use email when they start their careers.

Email encryption on both ends would help with identification and privacy. And I’m not that familiar with Google Wave; maybe it’s a better system. But your suggestions like “use Twitter instead!” are just not practical for many use cases.

So here’s a post I’d love to see: Jeff’s detailed proposal to replace email with something better.

That’s ruby code? It looks like Pascal. :slight_smile:

And that’s just the beginning! To really improve your deliverability you have to do bounce processing right. Which is a total pain. I therefore would strongly encourage folks to check out providers. I particularly like

I have to say that I share your feelings about e-mail, but what are the alternatives?

I suppose there are a few situations where emails are sent that you actually care about:

  1. Someone else performs some action that you want to know about, so you can respond - posts an answer, turn, whatever.

  2. You have forgotten your password and need a new one.

And actually, that’s about it. #2 can be circumvented by using a system like OpenID - essentially “make it someone else’s problem”.

#1 is nebulous and difficult - you could argue that RSS feeds are a solution, but to take a system like Stack Overflow as an example, I don’t want to have to subscribe to a feed for every question I ask/answer to get updates for it. Maybe you could have a ‘personal’ feed that updates only things relevant for me. Actually, I quite like that as a solution, not something I’ve seen (noticed?) implemented. Not quite so useful in situations where you might use a service once or twice though.

My Inbox is almost exclusively used as a notification area - things happen on one of the various websites I use, my iPhone beeps to tell me I have an email. I respond when I have a moment - all the while being reminded by my unread count that I have some action to take at some point. Often though, an email arrives that is just some boring marketing thing or a spam that got through or a chain letter from my mum, resulting in a false-positive beep from my iPhone.

RSS doesn’t quite cut this mustard - although some of that may be down to the way most RSS clients are implemented. It also doesn’t help for the idiots and old people who only have e-mail, but maybe we should start treating them like IE 6 users - just pretend they don’t exist and hope they go away?

Sure email is crap but all you are saying here is “oh noes I have to follow the standards involved”. At least there are fairly universal standards.

Easy to say that when Jeff’s just laid them out for us. I suspect many, many hours of painful trial and error went into this blog post.

Can you propose a better system? It has to meet the following requirements

  1. Your address can be public, so anybody can send to you without having an explicit prior relationship. This opens the door for spammers, but also job applicants, customers, etc.

Does your address have to be public? Everyone I know who isn’t into computers like we are (which, remember, is likely 95–99% of the people in the world, and is therefore the kind of person that matters) uses Facebook instead of e-mail. There’s no real concept of a public address for messages there. You have to explicitly allow people to send you messages by becoming their friend.