Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
aalbagarcia opened this issue Apr 12, 2017 · 7 comments

Comments

@aalbagarcia
Copy link

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
Copy link
Contributor

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
Copy link
Author

@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
Copy link
Contributor

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

Keep you posted on our progress here

@Lorel
Copy link
Contributor

Lorel commented Apr 27, 2017

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

@arnaudbreton
Copy link
Contributor

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
Copy link
Author

@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
Copy link
Contributor

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants