Created
April 14, 2015 02:50
-
-
Save komasaru/68820f52b874bb9e614d to your computer and use it in GitHub Desktop.
Ruby script to send a ISO-2022-JP mail.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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