File: comment_mail-smtp.rb

package info (click to toggle)
tdiary 5.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,088 kB
  • sloc: ruby: 23,031; javascript: 1,029; xml: 325; makefile: 26; sh: 4
file content (77 lines) | stat: -rw-r--r-- 2,952 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# comment_mail-smtp.rb
#
# SMTPプロトコルを使ってツッコミをメールで知らせる
#   同一ホスト内にSMTPサーバがある場合は有効にするだけで動作する
#
# Options:
#   設定画面から指定できるもの(ツッコミメール系プラグイン共通):
#     @options['comment_mail.enable']
#          メールを送るかどうかを指定する。true(送る)かfalse(送らない)。
#          無指定時はfalse。
#     @options['comment_mail.header']
#          メールのSubjectに使う文字列。振り分け等に便利なように指定する。
#          実際のSubjectは「指定文字列:日付-1」のように、日付とコメント番号が
#          付く。ただし指定文字列中に、%に続く英字があった場合、それを
#          日付フォーマット指定を見なす。つまり「日付」の部分は
#          自動的に付加されなくなる(コメント番号は付加される)。
#          無指定時には空文字。
#     @options['comment_mail.receivers']
#          メールを送るアドレス文字列。カンマで区切って複数指定できる。
#          無指定時には日記筆者のアドレスになる。
#
#   tdiary.confでのみ指定できるもの:
#     @options['comment_mail.smtp_host']
#     @options['comment_mail.smtp_port']
#          それぞれ、メール送信に使うSMTPサーバのホスト名とポート番号。
#          無指定時はそれぞれ「'localhost'」と「25」。
#   以下は通常は不要。必要に応じて指定する:
#     @options['comment_mail.user_name']
#     @options['comment_mail.password']
#          SMTP認証が必要な場合のユーザ名とパスワード
#     @options['comment_mail.authentication']
#          SMTP認証の方式。:plainや:loginなど(Mail gemに指定できるもの)
#
# Copyright (c) 2015 TADA Tadashi <t@tdtds.jp>
# You can distribute this file under the GPL2 or any later version.
#
def comment_mail(text, to)
	begin
		require 'mail'
		mail = Mail.new(text)
		mail.delivery_method(:smtp,
			address: @conf['comment_mail.smtp_host'] || 'localhost',
			port: @conf['comment_mail.smtp_port'] || 25,
			authentication: @conf['comment_mail.authentication'],
			user_name: @conf['comment_mail.user_name'],
			password: @conf['comment_mail.password']
		)
	rescue
		$stderr.puts $!
	end
	begin
		mail.deliver
	rescue
		# retry without verify when SSL error
		unless mail.delivery_method.settings[:openssl_verify_mode] == 'none'
			mail.delivery_method(:smtp, openssl_verify_mode: 'none')
			retry
		end
		$stderr.puts $!
	end
end

add_update_proc do
	comment_mail_send if @mode == 'comment'
end

add_conf_proc( 'comment_mail', comment_mail_conf_label, 'tsukkomi' ) do
	comment_mail_basic_setting
	comment_mail_basic_html
end

# Local Variables:
# mode: ruby
# indent-tabs-mode: t
# tab-width: 3
# ruby-indent-level: 3
# End: