-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbiz_mail_reminder.rb
149 lines (122 loc) · 3.77 KB
/
biz_mail_reminder.rb
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
require 'rubygems'
require 'yaml'
require 'date'
require 'date-util'
require 'tmail'
require 'tmail-util'
require 'erb'
class BizMailReminder
def initialize(user)
config = YAML.load_file($CONFIG_YAML)
@report_user = user
@current_config = config[user]
@person = ''
@kpi = ''
erb = ERB.new(File.read($BIZMAIL_DIR + @current_config['remind']))
erb.def_method(BizMailReminder, :_gen_remind, @current_config['remind'])
end
def remind_to_person(&block)
return if @current_config['type'] != 'aggregated_persons'
@current_config['remind_to'].each do |k, v|
tmail = TMail::Mail.new
tmail.content_type = 'text/plain'
tmail.charset = 'iso-2022-jp'
tmail.date = Time.now
kpi_id = ''
@current_config['kpi'].each do |_k, _v|
kpi_id = _k
@kpi = _v
end
tmail.from = [@report_user + '+' + [date.to_YYYYMMDD, kpi_id, k].join('-') + '@' + $MYDOMAIN]
tmail.reply_to = tmail.from
@person = @current_config['person'][k]
tmail.to = v
reminder = _gen_remind.split("\n")
subject = reminder[0]
reminder.shift
body = reminder.join("\n")
tmail.subject = '=?ISO-2022-JP?B?' + NKF.nkf('--jis', subject).split(//, 1).pack('m').chomp + '?='
tmail.body = NKF.nkf('--jis', body)
block.call(tmail)
end
end
def remind_for_kpi(&block)
return unless @current_config['type'] == 'multi_kpi' or @current_config['type'] == 'single_kpi'
@person = nil
tmail = TMail::Mail.new
tmail.content_type = 'text/plain'
tmail.charset = 'iso-2022-jp'
tmail.date = Time.now
@current_config['kpi'].each do |k, v|
@kpi = v
tmail.from = [@report_user + '+' + [date.to_YYYYMMDD, k, ''].join('-') + '@' + $MYDOMAIN]
tmail.reply_to = tmail.from
tmail.to = @current_config['remind_to']
reminder = _gen_remind.split("\n")
subject = reminder[0]
reminder.shift
body = reminder.join("\n")
tmail.subject = '=?ISO-2022-JP?B?' + NKF.nkf('--jis', subject).split(//, 1).pack('m').chomp + '?='
tmail.body = NKF.nkf('--jis', body)
block.call(tmail)
end
end
def remind_for_item(&block)
return if @current_config['type'] != 'aggregated_items'
@person = nil
tmail = TMail::Mail.new
tmail.content_type = 'text/plain'
tmail.charset = 'iso-2022-jp'
tmail.date = Time.now
kpi_id = ''
@current_config['kpi'].each do |_k, _v|
kpi_id = _k
@kpi = _v
end
@current_config['item'].each do |k, v|
@item = v
tmail.from = [@report_user + '+' + [date.to_YYYYMMDD, kpi_id, k].join('-') + '@' + $MYDOMAIN]
tmail.reply_to = tmail.from
tmail.to = @current_config['remind_to']
reminder = _gen_remind.split("\n")
subject = reminder[0]
reminder.shift
body = reminder.join("\n")
tmail.subject = '=?ISO-2022-JP?B?' + NKF.nkf('--jis', subject).split(//, 1).pack('m').chomp + '?='
tmail.body = NKF.nkf('--jis', body)
block.call(tmail)
end
end
def date
offset = 0
offset = 1 if @current_config['remind_date'] == 'yesterday'
return Date.today - offset
end
def item
return @item
end
def person
return @person
end
def kpi
return @kpi
end
end
if __FILE__ == $0 then
require 'rubygems'
$CONFIG_YAML = "/Users/hiropipi/Documents/workspace/BizMail/config.yaml"
$MYDOMAIN = 'dmail.pgw.jp'
my_reminder = BizMailReminder.new('user01')
my_reminder.remind_to_person do |tmail|
tmail.smtp_server = 'localhost'
tmail.write_back
puts NKF.nkf('--utf8', tmail.encoded)
#tmail.send_mail
end
my_reminder.remind_for_kpi do |tmail|
tmail.smtp_server = 'localhost'
tmail.write_back
puts NKF.nkf('--utf8', tmail.encoded)
#tmail.send_mail
end
end