0

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

0 回答 0