Send an email from gmail with Ruby

Introduction

As an exercise to write a program at the study session I am attending, there is a theme of sending an email with ruby, and the details of the exercise are described below.

By the way, it is a program sent from gmail that I usually use.

It was an issue while there was a time limit, so I hurriedly carried it out, but I was helped by having more references and libraries than I expected.

Implementation environment, what you need

Implementation procedure

Get Google App Password

For authentication when sending an email from the app, instead of using the google account password as it is, authenticate using the app password issued in the google account settings. (Recommended method for security rather than using the password as it is)

Account settings

From the above screen, select "Security"-> "App Password", select App: Email, Device (mac in my case), and then press the Generate button to display the App Password.

Install ruby-gmail gem

Thankfully, ruby has a gem for sending gmail, so I will use this.

dcparker/ruby-gmail

gem install ruby-gmail

ruby-gmail gem seems to be interesting because it has various functions such as reading and editing received mails in addition to simply sending mails.

Environment variable settings (gmail address, app password)

From the viewpoint of security, the gmail address and the application password obtained earlier will be set in the environment variable instead of being solidly written in the program, and will be obtained by the program.

export GMAIL_ID="The gmail address you want to send"
export GMAIL_PASS="gmail app password"

Reflects the set environment variables.

source ~/.bash_profile

Creating an email sending program

This time, you can enter any destination address, subject, and body on the command.

require "gmail"

USERNAME = ENV[ "GMAIL_ID" ]
PASSWORD = ENV[ "GMAIL_PASS"]

gmail = Gmail.new(USERNAME, PASSWORD)

puts "Please enter the destination address"
address_to = gets.chomp
puts "Please enter a subject"
title = gets.chomp
puts "Please enter the text"
text = gets

message =
  gmail.generate_message do
    to address_to
    subject title
    html_part do
      content_type "text/html; charset=UTF-8"
      body "<p>" + text + "</p>" 
    end
  end

gmail.deliver(message)
gmail.logout

Reference article

I referred to this for most of the program. I am very grateful.

[[Ruby] Send Email with Gmail Account --TakBoy's Programming Note](https://csharpmagazine.hatenablog.com/entry/2019/12/13/%E3%80%90Ruby%E3%80%91Gmail%E3%82% A2% E3% 82% AB% E3% 82% A6% E3% 83% B3% E3% 83% 88% E3% 81% A7% E3% 83% A1% E3% 83% BC% E3% 83% AB% E9% 80% 81% E4% BF% A1)

Recommended Posts

Send an email from gmail with Ruby
Ruby: Send email with Starttls
Send an email with a PDF attachment via JavaMail
[Spring Boot] Send an email
Send email with spring boot
I want to manually send an authorization email with Devise
[Java] Send an email using Amazon SES
[Ruby] Get unique elements from an array
Send an email using JavaMail on AWS
Sample to create PDF from Excel with Ruby
[Ruby] Calculation by extracting elements from an array
I want to send an email in Java.
Send an email when an ERROR level log occurs with the logback SMTP Appender
How to output standard from an array with forEach
Send a command to Spigot from an external process
Install Ruby 3.0.0 with asdf
Getting Started with Ruby
From Java to Ruby !!
11th, Classification with Ruby
Evolve Eevee with Ruby
I made an interpreter (compiler?) With about 80 lines in Ruby.
Ruby on Rails Email automatic sending function setting (using gmail)
[Rails] Create an email sending function with ActionMailer (complete version)
[2020 version] How to send an email using Android Studio Javamail
What happens to instance variables when copying an instance with ruby
Ruby / rexml: Turn each with an element with the same name
I tried to create an API to get data from a spreadsheet in Ruby (with service account)