Closed aalbagarcia closed 7 years ago
Hi @aalbagarcia, apologies for the delay in our response.
You can reconfigure the wrapper as shown here before any API call or set of calls on a given API key but I'm not comfortable with the thread safety of this method. Depending on your App setup, you could run in unexpected issues should you have different part of your app modifying the global wrapper configuration.
We're planning to move this configuration at the instance level - like the rest of our wrappers - allowing you to go beyond this issue.
What do you think? Apologies for the inconvenience this issue may cause you.
@arnaudbreton I cannot reconfigure the wrapper; we are using sidekiq to send the emails and as you said, I could run into unexpected behaviour because it's not thread safe. While you move that configuration to the instance level, I'm using faraday to send the requests using the REST API.
Looking forward to seeing this implemented.
Thanks
It sounds like a good workaround for now, apologies for the inconvenience.
Keep you posted on our progress here
Hello @aalbagarcia
When you create a message with a mailer, this one instantiates its delivery method based on your global configuration (e.g. an instance of Mailjet::Mailer
if you are using the delivery method :mailjet
)
Actually, Rails provides already two ways for overriding the delivery method and its configuration at runtime (but this is not well documented):
either by passing attributes :delivery_method
and :delivery_method_options
to the method mail
from your Rails mailer (here in the source code)
or by using the instance method :delivery_method
from the class Mail::Message
(before delivering your message)
If you are using the delivery method :mailjet
, Mailjet::Mailer
inheriting from Mail::SMTP
, you can already override this configuration, like you could do with the delivery method :smtp
, but using attribute names for :smtp
configuration :
from the method mail
:
class MyMailer < ApplicationMailer
def my_mail
# create email
api_key = ...
secret_key = ...
mail to: "recipient@email.com", delivery_method: :mailjet, delivery_method_options: { user_name: api_key, password: secret_key }
end
end
using the instance method :delivery_method
from the class Mail::Message
:
message = MyMailer.my_mail # create email
message.delivery_method( # override delivery method
Mailjet::Mailer,
{ user_name: api_key, password: secret_key }
)
message.deliver_now # send email
The gem will be improved soon to do the same using attribute names of Mailjet::Configuration
's configuration
If you are using the delivery method :mailjet_api
, things are a bit trickier, but it should be implemented in few days
I hope this can help
Hi @aalbagarcia, following up on @Lorel last comment. I've just merged his PR, #95 which contains the update to use credentials local to the instance.
Let us know if this works for you. It'll be part of the next release, v1.5.0
I'm planning to push later this week.
Stay tuned and thanks for having chosen Mailjet to power your emails!
@arnaudbreton I'm starting a course today till sunday so I don't think I'll have time to have a look at this until monday. Anyway, I had a look at @Lorel PR and it looks exactly what I was asking for so thanks a lot. I'll stay tuned.
@aalbagarcia thanks for the feedback, glad to hear this PR sounds to work for you. Good luck with your course, please keep us posted. Cheers
Hi,
We have different subaccounts defined in our mailjet user account. What we would like to do is from the same rails application, send user notifications through one of the subaccounts and send reports using another. Each subaccount has it's own API key and secret. My question is if it's possible to choose which subaccount to use when delivering the message.
According to the README, I can only define one API key and secret using an initializer so I see no way of doing it. I was wondering that maybe using the REST API I could do it.
Thanks