Basics of sending Gmail in Ruby

It's information about Ruby, but it's convenient when teaching Ruby, so a procedure memo

Get your Gmail app password

In short, I want the following 16 digits スクリーンショット 2021-01-10 9.03.09.png

The following work is done on the here screen.

  1. Enable Google two-step verification (otherwise you won't get your password!) -[Password and login method]-> [Two-step authentication process] --Authenticate with SMS and activate
  2. Get the app password -[Password and login method]-> [App password] -Enter [Select App] and [Select Device] respectively (anything that you can manage ... should be) --The app password will be generated, so make a note of it.

Install gem mail

$ gem mail install

Write sample code

send_gmail.rb


require 'mail'

from   = '[email protected]'
password = '16_digit_app_password'
to = '[email protected]'

Mail.defaults do
  delivery_method :smtp, {
    address: 'smtp.gmail.com',
    port: 587,
    domain: 'example.com',
    user_name: from,
    password: password,
    authentication: :login,
    enable_starttls_auto: true
  }
end

m = Mail.new do
  from "#{from}"
  to "#{to}"
  subject "Great Mail Title"
  body "Fantastic body blar blar"
end

m.charset = "UTF-8"
m.content_transfer_encoding = "8bit"
m.deliver

It should work with this for the time being.

Reference information

Send email via Gmail using Ruby http://1bed.allright.life/?p=2004

Recommended Posts

Basics of sending Gmail in Ruby
Basics of Ruby
Judgment of fractions in Ruby
Ruby basics
Ruby basics
Implementation of ls command in Ruby
Basics of Ruby ~ Review of confusing parts ~
Ruby Basics 2 ~ Review of confusing parts ~
Ruby basics
Directory information of DEFAULT_CERT_FILE in Mac ruby 2.0.0
[GCD] Basics of parallel programming in Swift
Summary of hashes and symbols in Ruby
[Ruby] Classification and usage of loops in Ruby
Class in Ruby
[Ruby basics] About the role of true and break in the while statement
Heavy in Ruby! ??
[Ruby] Summary of class definitions. Master the basics.
[Ruby] Behavior of evaluation of conditional expression in while
Recommendation of Service class in Ruby on Rails
Ruby on Rails ~ Basics of MVC and Router ~
Basics of threads and Callable in Java [Beginner]
Enumerate subsets of arrays given in Ruby (+ α)
Create a native extension of Ruby in Rust
Count the number of occurrences of a string in Ruby
[For beginners] DI ~ The basics of DI and DI in Spring ~
[Ruby] The role of subscripts in learning elements in arrays
About eval in Ruby
[Ruby] Exception handling basics
Ruby on Rails basics
definition of ruby method
Basics of try-with-resources statement
Output triangle in Ruby
[Ruby] Class nesting, inheritance, and the basics of self
Variable type in ruby
Get the URL of the HTTP redirect destination in Ruby
Output in multiples of 3
Fast popcount in Ruby
Handling of date and time in Ruby. Use Date and Time properly.
Ruby on Rails Email automatic sending function setting (using gmail)
Determine that the value is a multiple of 〇 in Ruby
Microbenchmark for integer power of floating point numbers in Ruby
Handling of line beginning and line ending in regular expressions in Ruby
Basic methods of Ruby hashes
ABC177 --solving E in Ruby
Validate JWT token in Ruby
Implemented XPath 1.0 parser in Ruby
[Rails] Introduction of devise Basics
Docker monitoring-explaining the basics of basics-
Basic methods of Ruby arrays
Read design patterns in Ruby
[GCD] Basics of DispatchQueue class
Basics of character operation (java)
Write class inheritance in Ruby
[Ruby] Various types of each
Understand the basics of docker
Update Ruby in Unicorn environment
Integer unified into Integer in Ruby 2.4
[Ruby] Exception handling in functions
Use ruby variables in javascript.
Multiplication in a Ruby array
About regular expressions in Ruby