Rails / Ruby: How to get HTML text for Mail

In Rails, the Mailer class, which inherits ActionMailer :: Base, uses a method called mail to create a mail object. Calling the method deliver on the return value mail object will send the mail.

Get HTML as text

To get the body of this HTML email as text, call the method as follows:

mail.html_part.body.to_s

This html_part is defined in the Gem mail used by Rails.

2020-11-Code as of 14


    # Accessor for html_part
    def html_part(&block)
      if block_given?
        self.html_part = Mail::Part.new(:content_type => 'text/html', &block)
      else
        @html_part || find_first_mime_type('text/html')
      end
    end

Get the body of Plain Text

You can use text_part to get it in Plain Text.

mail.text_part.body.to_s

Up to .body will return an instance of Body class.

Relation

This article describes how to get the body from the return value of ActionMailer's mail method. If you want to get the text after sending it, the following article will be helpful.

Recommended Posts

Rails / Ruby: How to get HTML text for Mail
How to use Ruby on Rails
How to get along with Rails
How to get date data in Ruby
[Ruby] How to use slice for beginners
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] How to use kaminari
[For beginners] How to get the Ruby delayed railway line name
[Ruby on Rails] How to avoid creating unnecessary routes for devise
[For Rails beginners] Summary of how to use RSpec (get an overview)
[Rails] How to implement unit tests for models
[Rails] How to use Gem'rails-i18n' for Japanese support
[Ruby on Rails] How to install Bootstrap in Rails
[Rails] How to get success and error messages
[Ruby on Rails] How to use session method
How to implement Pagination in GraphQL (for ruby)
How to write Rails
How to uninstall Rails
How to implement login request processing (Rails / for beginners)
[Rails] How to create a signed URL for CloudFront
[Ruby on Rails] How to write enum in Japanese
[Ruby on Rails] How to change the column name
[Ruby On Rails] How to reset DB in Heroku
[Rails] How to get the contents of strong parameters
[Ruby] How to get the tens place and the ones place
(Ruby on Rails6) How to create models and tables
How to get started with creating a Rails app
[Ruby On Rails] How to use simple_format to display the entered text with line breaks
[Ruby on Rails] How to implement tagging / incremental search function for posts (without gem)
Explanation of Ruby on rails for beginners ④ ~ Naming convention and how to use form_Tag ~
[rails] How to post images
[Rails] How to use enum
How to use Ruby return
[Rails] How to install devise
[Rails] How to use enum
How to read rails routes
[Ruby] How to comment out
How to use rails join
Ruby: How to use cookies
How to terminate rails server
How to write Rails validation
How to write Rails seed
[Rails] How to use validation
[Ruby] How to write blocks
[Rails] How to disable turbolinks
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to implement scraping
[Rails] How to make seed
How to write Rails routing
[Rails] How to install simple_calendar
[Rails] How to install reCAPTCHA
[Rails] How to use Scope
[For Ruby beginners] Explain how to freely delete array elements!
How to display a graph in Ruby on Rails (LazyHighChart)
[Rails] How to display error messages for comment function (for beginners)
[Rails] How to use gem "devise"
How to use Java HttpClient (Get)
How to deploy jQuery on Rails
[Rails] How to install Font Awesome
[Rails] How to use devise (Note)