python 使用Django 的 邮件模块 发送邮件

python 有自己的邮件模块 smtplib,但是Django对其进行了封闭,感觉更简单了,使用DJANGO 邮件模块,两三行代码就可以解决问题。

def sendMail(subject, message, to_list, attachments=None, cc=None):
    """
   @subject: 邮件标题
   @message:邮件内容,可以是html字符串
   @to_list:是一个列表,接收邮件的用户列表,都是邮箱
   @attachments附件
    """
    from django.core.mail.message import EmailMessage
    from django.core.mail import get_connection
    from products.netMinWeb import settings
    
    from_email = auth_user = settings.EMAIL_HOST_USER #从django配置,发送者的邮箱
    auth_password = settings.EMAIL_HOST_PASSWORD    #从django配置,发送者的邮箱密码
    headers = {                                                                  #邮件头
               "Date": time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
    }
    connection = get_connection(username=auth_user, password=auth_password, fail_silently=False) #连接到发送者邮箱
    emailMsg = EmailMessage(subject, message, from_email, to_list, connection=connection, attachments=None, headers=headers, cc=None)
    emailMsg.content_subtype = "html" #文本内容类型,默认是“plain”
    emailMsg.send()


 DJANGO EmailMessage 类部分代码: 

class EmailMessage(object):
    """
    A container for email information.
    """
    content_subtype = 'plain'
    mixed_subtype = 'mixed'
    encoding = None     # None => use settings default

    def __init__(self, subject='', body='', from_email=None, to=None, bcc=None,
                 connection=None, attachments=None, headers=None, cc=None):
        """
        Initialize a single email message (which can be sent to multiple
        recipients).

        All strings used to create the message can be unicode strings
        (or UTF-8 bytestrings). The SafeMIMEText class will handle any
        necessary encoding conversions.
        """
        if to:
            assert not isinstance(to, six.string_types), '"to" argument must be a list or tuple'
            self.to = list(to)
        else:
            self.to = []
        if cc:
            assert not isinstance(cc, six.string_types), '"cc" argument must be a list or tuple'
            self.cc = list(cc)
        else:
            self.cc = []
        if bcc:
            assert not isinstance(bcc, six.string_types), '"bcc" argument must be a list or tuple'
            self.bcc = list(bcc)
        else:
            self.bcc = []
        self.from_email = from_email or settings.DEFAULT_FROM_EMAIL
        self.subject = subject
        self.body = body
        self.attachments = attachments or []
        self.extra_headers = headers or {}
        self.connection = connection

    def get_connection(self, fail_silently=False):
        from django.core.mail import get_connection
        if not self.connection:
            self.connection = get_connection(fail_silently=fail_silently)
        return self.connection


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值