Login

SMTPConnection with Return-Path

Author:
sgb
Posted:
May 2, 2008
Language:
Python
Version:
.96
Tags:
email
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. Function to create resized versions of an image from a URL and saving it to a local path by obeattie 8 years, 3 months ago
  2. EasyFeed class by limodou 8 years, 3 months ago
  3. Email queue in DB by fish2000 5 years, 1 month ago
  4. Multiple emails form field by matrix 1 year, 5 months ago
  5. SSL Middleware by sjzabel 8 years, 2 months ago

Comments

Please login first before commenting.