tasks.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:django-danceschool 作者: django-danceschool 项目源码 文件源码
def sendEmail(subject,content,from_address,from_name='',to=[],cc=[],bcc=[],attachment_name='attachment',attachment=None,html_content=None):
    # Ensure that email address information is in list form and that there are no empty values
    recipients = [x for x in to + cc if x]
    bcc = [x for x in bcc if x]
    from_email = from_name + ' <' + from_address + '>' if from_address else None
    reply_to = [from_address,] if from_address else None

    logger.info('Sending email from %s to %s' % (from_address,recipients))

    if getattr(settings,'DEBUG',None):
        logger.info('Email content:\n\n%s' % content)
        logger.info('Email HTML content:\n\n%s' % html_content)

    with get_connection() as connection:
        connection.open()

        message = EmailMultiAlternatives(
            subject=subject,
            body=content,
            from_email=from_email,
            to=recipients,
            bcc=bcc,
            reply_to=reply_to,
            connection=connection,
        )

        if html_content:
            message.attach_alternative(html_content, "text/html")

        if attachment:
            message.attach(attachment_name, attachment)

        message.send(fail_silently=False)
        connection.close()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号