How could I pass parameter to this block

60 Views Asked by At

I want to send an email every day with a different attachment. So I think I should pass the parameter to mail's initialize block. I read some articles about ruby block but I can not find the right way to implement it. How could I do it the right way and why does the mail's initialize method pass a block? Thank you.

require 'mail'
class MailSender
  attr_accessor :created_at
  def initialize
    delivery_options = { 
      address: 'xxmail.com',
      port: 25,
      user_name: '[email protected]',
      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 '[email protected]'
      to '[email protected]'
      subject 'mailtest'
      body 'The first mail.'
      add_file :filename => "#{created_at}.txt", :content => File.read("#{created_at}.txt")
    end
    mail.deliver!
  end
end
0

There are 0 best solutions below