Created
          February 23, 2011 19:33 
        
      - 
      
- 
        Save niran/840999 to your computer and use it in GitHub Desktop. 
    Multiple email connections in Django
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | from django.conf import settings | |
| import django.core.mail | |
| class MissingConnectionException(Exception): | |
| pass | |
| def get_connection(label=None, **kwargs): | |
| if label is None: | |
| label = getattr(settings, 'EMAIL_CONNECTION_DEFAULT', None) | |
| try: | |
| connections = getattr(settings, 'EMAIL_CONNECTIONS') | |
| options = connections[label] | |
| except KeyError, AttributeError: | |
| raise MissingConnectionException( | |
| 'Settings for connection "%s" were not found' % label) | |
| options.update(kwargs) | |
| return django.core.mail.get_connection(**options) | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | EMAIL_CONNECTIONS = { | |
| 'gmail': { | |
| 'host': 'smtp.gmail.com', | |
| 'username': '[email protected]', | |
| 'password': 'password', | |
| 'port': 587, | |
| 'use_tls': True, | |
| }, | |
| 'socketlabs': { | |
| 'host': 'smtp.socketlabs-od.com', | |
| 'username': 'blah', | |
| 'password': 'password', | |
| 'use_tls': False, | |
| }, | |
| 'ses': { | |
| 'backend': 'django_ses.SESBackend', | |
| }, | |
| 'distributed': { | |
| 'backend': 'tt.mail.backends.distributed.DistributedBackend', | |
| 'components': ( | |
| # Use SES for 8% of our emails. We send about 10,000 per day, so | |
| # 8% should keep us under our initial quota of 1,000. | |
| ('ses', 0.08), | |
| ('socketlabs', 0.92), | |
| ), | |
| } | |
| } | |
| EMAIL_CONNECTION_DEFAULT = 'distributed' | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment