Login

SMTPConnection with Return-Path

Author:
sgb
Posted:
May 2, 2008
Language:
Python
Version:
.96
Score:
2 (after 2 ratings)

Sometimes when sending email you want to specify a Return-Path different from the "From:" address on the email. The Return-Path is used for bounced email and is required for VERP.

This class overrides SMTPConnection so that you can specify return_path.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
from django.core.mail import SMTPConnection

class CustomSMTPConnection(SMTPConnection):
    """Simple override of SMTPConnection to allow a Return-Path to be specified"""
    def __init__(self, return_path=None, **kwargs):
        self.return_path = return_path
        super(CustomSMTPConnection, self).__init__(**kwargs)
    
    def _send(self, email_message):
        """A helper method that does the actual sending."""
        if not email_message.to:
            return False
        try:
            return_path = self.return_path or email_message.from_email
            self.connection.sendmail(return_path,
                    email_message.recipients(),
                    email_message.message().as_string())
        except:
            if not self.fail_silently:
                raise
            return False
        return True

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Please login first before commenting.