mailjet / mailjet-gem

[API v3] Mailjet official Ruby GEM
https://dev.mailjet.com
Other
130 stars 72 forks source link

Is it possible to send emails through different subaccounts inside a single rails application? #91

Closed aalbagarcia closed 7 years ago

aalbagarcia commented 7 years ago

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

arnaudbreton commented 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.

aalbagarcia commented 7 years ago

@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

arnaudbreton commented 7 years ago

It sounds like a good workaround for now, apologies for the inconvenience.

Keep you posted on our progress here

Lorel commented 7 years ago

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):

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 :

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

arnaudbreton commented 7 years ago

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!

aalbagarcia commented 7 years ago

@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.

arnaudbreton commented 7 years ago

@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