Last week I released Supermail, a gem that makes working with emails in Rails much more intuitive by using a much simpler abstraction. Something I love about simpler abstractions is they often lead to pleasant discoveries, in my case creating mailto links with an instance of an email. This week I had the revelation I could use those email objects to generate mailto links.
Generating support emails
The Beautiful Ruby checkout flow has a “cancel” state where I’m assuming the person trying to buy the Phlex on Rails video course ran into problems checking out. Instead of dumping them off into a dead-end, I give them an option to email me to get help. Here’s what that looks like in Supermail.
# ./app/emails/support_email.rb
class SupportEmail < ApplicationEmail
def initialize(checkout_session:)
@checkout_session = checkout_session
end
def to = from
def subject = "Payment issue on Beautiful Ruby"
def body = <<~BODY
Hi,
I had trouble completing my payment.
Here's the checkout session ID: #{@checkout_session.id}
BODY
end
# Then from your views...
<%= link_to SupportEmail.new(checkout_session: @checkout_session).mailto, "Get help with your payment" %>
The @checkout_session
is an instance of a Stripe::Checkout::Session
object from the Stripe Gem.
Generating mailto links
Here’s where the power of this simple abstraction lands: I can use it to generate mailto links that launch the users’ email clients with an instance of an email.
<%= link_to SupportEmail.new(checkout_session: @checkout_session).mailto, "Get help with your payment" %>
The HTML it generates for the link looks like this:
<a href="mailto:beautifulruby@example.com?subject=Payment%20issue%20on%20Beautiful%20Ruby&body=Hi%0A%0AI%20had%20trouble%20completing%20my%20payment.%0A%0AHere's%20the%20checkout%20session%20ID:%20%7B%40checkout_session.id%7D">Email Beautiful Ruby</a>
This instructs the user’s browser to launch their email client. On my machine the message looks like this:
The customer hits “Send” and boom, I get to connect with the person on a human level and work out the problem with payment processing.