[Ruby] Ruby: Send email with Starttls

less than 1 minute read

I did the same thing here in Ruby. Python3: Send email with Starttls I tried it on hi-ho.ne.jp.

Library installation

sudo gem install mail

hi-ho.rb


#! /usr/bin/ruby
# -*- encoding: utf-8 -*-
#
# hi-ho.rb
#
# Jul/08/2020
#
# ------------------------------------------------- --------------------
require "mail"
require "dotenv"

STDERR.puts "*** start ***"

Dotenv.load
server = ENV['SERVER']
port = ENV['PORT']
usr = ENV['USR']
password = ENV['PASSWORD']
from = ENV['FROM']
to = ENV['TO']
#
puts server
puts port

str_out = "Good Morning\n"
str_out + = "Hello. \ n"
str_out += "Jul/08/2020\n"
str_out += "PM 15:58\n"

mail = Mail.new do
  from from
  to to
  subject "Hello from Hi-ho PM 15:58"
  content_type'text/plain; charset=UTF-8'
  body str_out
end

mail.delivery_method(:smtp,
  address: server,
  port: port,
  authentication: :login,
  us
er_name: usr,
  password: password
)

mail.deliver

STDERR.puts "*** end ***"
# ------------------------------------------------- --------------------

.env


SERVER ='hi-ho.mose-mail.jp'
PORT = 587
USR ='****@hi-ho.ne.jp'
PASSWORD ='****'
FROM ='****@hi-ho.ne.jp'
TO ='[email protected]'

Execution result

$ ./hi-ho.rb
*** Start ***
hi-ho.mose-mail.jp
587
*** End ***

I confirmed in the next version.

$ ruby --version
ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux-gnu]