0

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>
Nakilon
  • 34,866
  • 14
  • 107
  • 142

1 Answers1

1

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
Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
  • Oh, this is what I was afraid of ) thank you for investigation! Do you recommend another library? – Nakilon Aug 13 '19 at 08:43
  • I have no idea. I am not a big fan of using 3rd party hems, they always have glitches. BTW, what would be wrong with the workaround I proposed? You might dig in, or even fork and modify it to support multiple retrievers; it should not be complicated or time-consuming. Simply replicate functionality it has for deliveries—and voilà. – Aleksei Matiushkin Aug 13 '19 at 10:01