Giter Club home page Giter Club logo

Comments (2)

medmunds avatar medmunds commented on May 24, 2024

@avelis could you give a little more detail of your scenario?

There are a couple of ways Anymail could offer this:

  1. As a per-message override, probably in the esp_extra dict. (Similar to how it handles overriding Mailgun's sender_domain.) Something like:

    message = EmailMessage(...)
    message.esp_extra = {"username": "OtherApiUser", "password": "..."}  # or "api_key": "..."
    message.send()
  2. As a connection-specific override, which would apply to all emails sent using that connection:

    message1 = EmailMessage(...)
    message2 = EmailMessage(...)
    connection = get_connection('anymail.backends.sendgrid.SendGridBackend',
                                username="OtherApiUser", password="...")  # or api_key="..."
    connection.send_messages([message1, message2])

The first approach is probably easier to explain and use. The second approach is (I think) more in keeping with the spirit of Django email backend connections, and is more likely to be portable to other ESPs and API bindings. (Which is why it would help to understand if this is a SendGrid-specific scenario or more generally applicable.)

from django-anymail.

avelis avatar avelis commented on May 24, 2024

@medmunds Good Question! I customized a solution almost exactly like Option 2.

I basically subclassed django-anymail's SendGridBackend as such:

class SendGridRuntimeAuthenticationBackend(SendGridBackend):

    def __init__(self, **kwargs):
        """Init options from Django settings"""
        # Auth requires *either* SENDGRID_API_KEY or SENDGRID_USERNAME+SENDGRID_PASSWORD
        self.api_key = get_anymail_setting('SENDGRID_API_KEY', default=None, allow_bare=True)
        self.username = get_anymail_setting('SENDGRID_USERNAME', default=None, allow_bare=True)
        self.password = get_anymail_setting('SENDGRID_PASSWORD', default=None, allow_bare=True)

        if 'auth_username' in kwargs and 'auth_password' in kwargs:
            # This is custom to our needs.
            self.api_key = None
            self.username = kwargs.get('auth_username', None)
            self.password = kwargs.get('auth_password', None)

        if self.api_key is None and self.username is None and self.password is None:
            raise ImproperlyConfigured(
                "You must set either SENDGRID_API_KEY or both SENDGRID_USERNAME and "
                "SENDGRID_PASSWORD in your Django ANYMAIL settings."
                "Optionally you can set 'auth_username' & 'auth_password' "
                "kwargs via the get_connection method."
            )

        # This is SendGrid's Web API v2 (because the Web API v3 doesn't support sending)
        api_url = get_anymail_setting("SENDGRID_API_URL", "https://api.sendgrid.com/api/")
        if not api_url.endswith("/"):
            api_url += "/"

        # This is a hack and not an ideal way to do this.
        AnymailRequestsBackend.__init__(self, api_url, **kwargs)

You will notice the hack comment I made. It helped me get around the improperly configured exception. However, the code smell is what prompted me to ask for this feature.

As far as I know. SendGrid has subusers that can store their own credentials along with the parent account. Mandrill only has subaccount slugs but that might change as it moves into MailChimp as an addon.

I am unsure which of your proposed options is a better solution at the moment. I am open to either one.

from django-anymail.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.