我想每天发送一封电子邮件,附上不同的附件。所以我认为我应该将参数传递给mail的初始化块。我阅读了一些关于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