Skip to content

Repository files navigation

Supermail

Organize emails with plain 'ol Ruby objects in a Rails application:

classUser::WelcomeEmail < ApplicationEmaildefinitialize(user:)@user=userenddefto=@user.emaildefsubject="Welcome to Beautiful Ruby"defplain=<<~TEXT Hi #{@user.name}, You're going to learn a ton at https://beautifulruby.com. TEXTdefhtml=safe <<~HTML
<h1>Hi#{@user.name},</h1>
<p>You'regoingtolearnatonat <ahref="https://beautifulruby.com">BeautifulRuby</a>.</p>
HTMLend

Or use ERB templates like Rails:

# app/emails/welcome_email.rbclassWelcomeEmail < ApplicationEmailincludeSupermail::Rails::Renderingdefinitialize(user:)@user=userenddefto=@user.emaildefsubject="Welcome!"# Templates automatically resolved from app/emails/welcome_email.html.erb and .text.erbend
<!-- app/emails/welcome_email.html.erb --><h1>Hi <%=@user.name%>,</h1><p>Welcome to the app!</p>

Contrast that with Rails ActionMailer, where you will spend 20 minutes trying to figure out how to send an email. I created this gem because I got tired of digging through Rails docs to understand how to initialize an email and send it. PORO's FTW!

Support this project

Learn how to build UI's out of Ruby classes and support this project by ordering the Phlex on Rails video course.

Installation

Install the gem and add to the application's Gemfile by executing:

bundle add supermail

Then install it in Rails.

rails generate supermail:install

This creates the ApplicationEmail class at app/emails/application_email.rb.

Usage

Basic emails

Define emails by overriding to, from, subject, plain, and html methods:

classWelcomeEmail < ApplicationEmaildefinitialize(user:)@user=userenddefto=@user.emaildefsubject="Welcome!"defplain="Welcome to the app, #{@user.name}!"defhtml=safe"<h1>Welcome to the app, #{@user.name}!</h1>"end
  • If both plain and html are defined, a multipart email is sent
  • If only plain is defined, a text-only email is sent
  • If only html is defined, an HTML-only email is sent

Wrapping content with hooks

Use before_* and after_* hooks in your base class to wrap all emails:

classApplicationEmail < Supermail::Rails::Basedeffrom="website@example.com"defbefore_html="<html><body>"defafter_html=<<~HTML<p>Best,<br>The Example.com Team</p></body></html> HTMLdefafter_plain=<<~TEXT Best, The Example.com Team TEXTend

Child emails just define their content - the hooks handle the wrapping:

classWelcomeEmail < ApplicationEmaildefsubject="Welcome!"defplain="Welcome to the app!"defhtml=safe"<h1>Welcome!</h1>"end

Using Phlex components

Return any object that responds to #call from html:

classWelcomeEmail < ApplicationEmaildefhtml=WelcomeEmailView.new(user: @user)end

CSS inlining

Override html_body for post-processing like CSS inlining:

classApplicationEmail < Supermail::Rails::Baseprotecteddefhtml_bodycontent=superreturnnilunlesscontentPremailer.new(content,with_html_string: true).to_inline_cssendend

Send emails

WelcomeEmail.new(user: User.first).deliver_nowWelcomeEmail.new(user: User.first).deliver_later

Generate mailto: links

<%= link_to "Contact Support", SupportEmail.new(user: current_user).mailto %>

Generators

Generate a new email:

rails generate supermail:email User::Welcome

Creates app/emails/user/welcome_email.rb.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/rubymonolith/supermail.

About

A better way to generate and send emails

Resources

Stars

26 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages