Comments (2)
@avelis could you give a little more detail of your scenario?
There are a couple of ways Anymail could offer this:
-
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()
-
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.
@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)
- Support Brevo inbound
- "Package would be ignored" warnings from setuptools when building package HOT 2
- Sendgrid multiple reply to HOT 1
- Can't lock with poetry HOT 2
- [Inbound] Apple Mail inline attachments not taken into account HOT 5
- Add support to Resend.com HOT 5
- Handlebars variables not substitued in mandrill templates HOT 1
- mailgun api key HOT 2
- Merging/Combine ESP Options get lost/overwritten HOT 2
- parse_address_list: Inability to Parse Email Addresses with Special Characters in Display Name Without Double-Quote Encapsulation HOT 1
- Mailjet Open tracking? HOT 4
- Inaccuracy in documentation about Brevo's behaviour HOT 2
- Feature request: Mailtrap HOT 4
- Resend: confusing ReadTimeout error for incorrect API key
- Add support to Unisender Go HOT 2
- Support Brevo batch sending with merge_data HOT 2
- Add batch sending support for Resend HOT 1
- Mailgun webhooks: AttributeError 'NoneType' object has no attribute 'get' HOT 1
- boto3 sesv2 client does not have method 'send_email'. Check AmazonSESV2SendEmailPayload HOT 4
- Bad UTF-8 "To" header encoding? HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-anymail.