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()
评论列表
文章目录