Skip to content

Latest commit

 

History

History
89 lines (56 loc) · 2.86 KB

README.md

File metadata and controls

89 lines (56 loc) · 2.86 KB

Maildown

Markdown for your ActionMailer generated emails. Supports Rails 5.0+

Build Status

What?

  • Fact: You should always send emails in text/html and text/plain at the same time
  • Fact: Writing email body content twice sucks
  • Fact: Markdown is amazing

So why not write your templates once in markdown, and have them translated to text and html? With Maildown now you can.

Install

Gemfile:

gem 'maildown'

Then run $ bundle install

Use

In your app/views/<mailer> directory create a file with a .md.erb extension. When rails renders the email, it will generate html by parsing the markdown, and generate plain text by sending the email as is.

Verify

Once you've got a file named .md.erb in your mailer directory, I recommend verifing the format in your browser in development using a tool such as mail_view. You can toggle between html and text at the top to make sure both look like you expect.

Configure Markdown Renderer

Maildown uses kramdown by default. Kramdown is pure ruby, so it runs the same across all ruby implementations: jruby, rubinius, MRI, etc. You can configure another parser if you like using the Maildown::MarkdownEngine.set_html method and pasing it a block.

For example, if you wanted to use Redcarpet you could set it like this:

Maildown::MarkdownEngine.set_html do |text|
  carpet = Redcarpet::Markdown.new(Redcarpet::Render::HTML, {})
  carpet.render(text).html_safe
end

When maildown needs an html document the block will be called with the markdown text. The result should be html.

You can also customize the renderer for plain text. By default the text is passed through unmodified, but you may wish to use Kramdown to strip HTML tags, unify formatting etc.

Maildown::MarkdownEngine.set_text do |text|
  Kramdown::Document.new(text).tap(&:to_remove_html_tags).to_kramdown
end

Helpers in Markdown files

To get great looking emails in both html and plaintext generate your own links like this:

[Your Profile](<%= user_url(@user) %>)

Instead of

<%= link_to "Your Profile", user_url(@user) %>

Bonus: it's shorter!

Future

This codebase depends on some metaprogramming to convince Action Mailer to render html and plain text from md. If you've got some ideas on how to add sane hooks into actionmailer to support this functionality more natively ping me @schneems

Alternative Implementations

There is another project that accomplishes the same thing by plataformatec: markerb.

License

MIT