rubyでタイミングメールを送信

32221 ワード

需要は簡単で、毎日いくつかのメールボックスに前日の販売データを送信します.
 
wheneverを使いたいの?sidetiq?それとも他に何かgem?
 
独立したrubyスクリプトを選択し、crontabが自動的に実行します.
 
baby.rb 1.56 KB
edit
raw
blame
history
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
require 'active_record'
require "net/smtp"
require 'mysql2'
require 'yaml'

config = YAML::load(File.open(File.dirname(__FILE__) + "/database.yml"))
ActiveRecord::Base.establish_connection(config)

def query_data
 ## debug
 # start_time = '2014-12-23 00:00:00'
 # end_time = '2014-12-23 23:59:59'
 start_time = (Date.today - 1).to_s + ' 00:00:00'
 end_time = (Date.today - 1).to_s + ' 23:59:59'
 sql = 'SELECT sum(h.order_amount) FROM shop_order as h' +' '+\
 'where (h.status<>-10 and h.status<>0)' +' '+\
 'and (h.pay_time>= "' + start_time + '" and h.pay_time<= "' + end_time + '")' +' '+\
 'and h.order_type="normal"'
 p sql
 ActiveRecord::Base.connection.select_value sql
end

def chinese_date
 "#{(Time.now - 1.day).year} #{(Time.now - 1.day).month} #{(Time.now - 1.day).day} "
end

def send_mail(data_user)
 from_address = "[email protected]"
 to_address = ["s—————@nipponpaint.com.cn", "w——@d——.com", "l——@d——.com"]

 mail = "Subject: 【    】" + chinese_date + "  \r
"
+\
"To: s————@nipponpaint.com.cn; w——@d——.com; l——@d——.com\r
"
+\
"Dear all,\r
"
+\
"\r
"
+\
" " + chinese_date + " :" + data_user.to_s + " " Net::SMTP.start('localhost') do |smtp| smtp.send_mail(mail, from_address, to_address) end end puts Time.new puts '----Job Start----' amount_user = query_data amount_user = '0' unless amount_user p "=>" + amount_user.to_s send_mail(amount_user) puts '----Job Finished----' puts Time.new exit;

 
crontab -e
1本加える
45 18 * * * /usr/local/bin/ruby /var/www/mail/ontime/baby.rb