1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Ruby: Starttls でメールの送信

Last updated at Posted at 2020-07-08

こちらと同じことを Ruby で行いました。
Python3: Starttls でメールの送信
hi-ho.ne.jp で試しました。

ライブラリーのインストール

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

STDERR.puts	"*** 開始 ***"

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 += "こんにちは。\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,
  user_name:      usr,
  password:       password
)

mail.deliver

STDERR.puts	"*** 終了 ***"
# ---------------------------------------------------------------------
.env
SERVER = 'hi-ho.mose-mail.jp'
PORT = 587
USR = '****@hi-ho.ne.jp'
PASSWORD = '****'
FROM = '****@hi-ho.ne.jp'
TO = 'sample@example.com'

実行結果

$ ./hi-ho.rb 
*** 開始 ***
hi-ho.mose-mail.jp
587
*** 終了 ***

次のバージョンで確認しました。

$ ruby --version
ruby 3.0.4p208 (2022-04-12 revision 3fa771dded) [x86_64-linux]
1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?