添付ファイルを変えて毎日メールを送りたいです。だから私はパラメータをメールの初期化ブロックに渡す必要があると思います。についていくつかの記事を読みましruby block
たが、それを実装する正しい方法が見つかりません。メールの初期化メソッドがブロックを渡すのはなぜですか? ありがとうございました。
require 'mail'
class MailSender
attr_accessor :created_at
def initialize
delivery_options = {
address: 'xxmail.com',
port: 25,
user_name: 'xxx@xxmail.com',
password: 'xxxxxx',
authentication: :login
}
Mail.defaults do
delivery_method :smtp, delivery_options
end
self.created_at = DateTime.now.prev_day.strftime("%F")
end
def notify
mail = Mail.new do
from 'xxx@xxmail.com'
to 'xxx@xxmail.com'
subject 'mailtest'
body 'The first mail.'
add_file :filename => "#{created_at}.txt", :content => File.read("#{created_at}.txt")
end
mail.deliver!
end
end