How do I set retriever_method not in defaults?

149 views Asked by At

Setting the delivery_method immediately within the delivery block instead of setting the defaults to send seems to work well with the following code:

    Mail.deliver do
      delivery_method :smtp,
        address: "smtp.gmail.com",
        domain: "#####",
        port: 587,
        user_name: from,
        password: "#####",
        enable_starttls_auto: true
      from     from
      to       to
      subject  subject
      body     "default body"
    end

But how do I do the same to read?

Mail.last do
  retriever_method :imap,
    user_name: to,
    password: password,
    address: server,
    port: port,
    enable_ssl: true
end

causes

*** NoMethodError Exception: undefined method `retriever_method' for #<RSpec::ExampleGroups::Nested::Nested_2::Nested::Email_2:0x00007fa246110478>
1

There are 1 answers

2
Aleksei Matiushkin On

FYI: this gem is OSS.

Mail#delivery_method is technically an alias to Configuration.instance.delivery_method.

Mail#retriever_method in turn redirects to Configuration.instance.retriever_method.

Unlike Mail::deliver, that creates a new instance, Mail::last explicitly calls retriever_method.last(*args, &block).

As one might see, instance allows to override delivery_method, but not retriever_method.

So you should store Configuration.instance.retriever_method into intermediate variable, update it, call Mail::last and restore it back. Somewhat along these lines:

Configuration.instance.instance_eval do
  generic_retriever_method = retriever_method
  retriever_method :imap,
    user_name: to,
    password: password,
    address: server,
    port: port,
    enable_ssl: true
  Mail.last
  retriever_method = generic_retriever_method
end