Skip to content

Instantly share code, notes, and snippets.

@komasaru
Created April 14, 2015 02:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save komasaru/68820f52b874bb9e614d to your computer and use it in GitHub Desktop.
Save komasaru/68820f52b874bb9e614d to your computer and use it in GitHub Desktop.
Ruby script to send a ISO-2022-JP mail.
#!/usr/local/bin/ruby
# coding: utf-8
#---------------------------------------------------
# メール送信(日本語(ISO-2022-JP)対応)
#
# Copyright(C) 2015 mk-mode.com All Rights Reserved.
#---------------------------------------------------
#
require 'mail'
require 'mail-iso-2022-jp'
class SendMail
def initialize
# メールオプジェクト生成
@mail = Mail.new(charset: 'ISO-2022-JP') do
from "hoge@xxxxxxxx.com"
to "fuga@yyyyyyyy.com"
subject "** TEST(テスト) **"
body <<-EOS
This is a test of mail sending.
これはメール送信テストです。
EOS
end
# オプション設定 (SMTP Over SSL)
# `password` の設定値を `($stdout.print "Password: "; gets.chomp)` に変更すると、
# 当スクリプト実行の都度パスワードの問い合わせを行う。
@smtp_opts = {
address: "smtp.xxxxxxx.com", # <= SMTP サーバのアドレス
port: 465, # <= Port 番号(単純な SMTP なら 25)
domain: "xxxxxxx.com", # <= ドメイン名
user_name: "hoge" , # <= 送信ユーザ名
password: "xxxxxxxx", # <= 送信ユーザパスワード
authentication: :plain, # <= 認証方式(コメントアウトしても問題ない)
ssl: true # <= SSL 認証(単純な SMTP なら false もしくはコメントアウト)
}
end
# メール送信
def send_mail
begin
@mail.delivery_method(:smtp, @smtp_opts)
@mail.deliver!
rescue => e
$stderr.puts "[#{e.class}] #{e.message}"
end
end
end
SendMail.new.send_mail unless __FILE__ == $0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment