背景
使用celery异步发邮件,加快系统速度。
不变的代码
from __future__ import absolute_import, unicode_literals
from celery import shared_task
import time
from celery import Celery
import exchangelib
from exchangelib import DELEGATE, Account, Credentials, Message, Mailbox, HTMLBody
from .models import *
webmail_items = webmail.objects.all()
'''邮件配置逻辑'''
'''1.设定默认值'''
'''2.读取配置值'''
'''3.本模块重写函数时,传值给原函数'''
'''4.view可再修改传值'''
# 不重要的信息
subject = 'Python测试发送邮件'
body = 'Dear, 这是python自动发送邮件测试'
Recipients = ['@wistron.com']
cc_list = ['@wistron.com']
# 定义邮件发送配置
if webmail_items:
Account1 = webmail_items.first().email_sender_account
Password = webmail_items.first().email_sender_password
primary_smtp_address=webmail_items.first().email_sender_address
Recipients = []
for x in webmail_items.first().hradmin_namelist.split(';'):
if x:
Recipients.append(Mailbox(email_address=x))
cc_list = []
for x in webmail_items.first().hradmin_namelist.split(';'):
if x:
cc_list.append(Mailbox(email_address=x))
else:
Account1 = 'wz'
Password = '@'
primary_smtp_address='@wistron.com'
# 申请后的签核提醒
def get_msg1(x='audit_query_booking_apply'):
msg = '''
<div>Dear all,</div>
<div>您收到一张教室预定申请的表单,请点击下方进行签核。</div>
<a href='http://127.0.0.1:8000/{0}/'>签核链接</a>
<div>承办人:WZS_HR</div>
<div>邮件为自动发送,请不要直接回复。</div>
'''.format(x)
return msg
# 签核同意
def get_msg2(x='query_booking_apply'):
msg = '''
<div>Dear all,</div>
<div>您的教室申请已被同意,请点击下方进行查看。</div>
<a href='http://127.0.0.1:8000/{0}/'>查看链接</a>
<div>承办人:WZS_HR</div>
<div>邮件为自动发送,请不要直接回复。</div>
'''.format(x)
return msg
# 签核拒绝
def get_msg3(x='query_booking_apply'):
msg = '''
<div>Dear all,</div>
<div>您的教室申请已被拒绝,请点击下方进行查看。</div>
<a href='http://127.0.0.1:8000/{0}/'>查看链接</a>
<div>承办人:WZS_HR</div>
<div>邮件为自动发送,请不要直接回复。</div>
'''.format(x)
return msg
# 5S检查结果查看
def get_msg4(x='query_booking_apply'):
msg = '''
<div>Dear all,</div>
<div>您的教室使用后,HR管理员对其进行检查,检查结果请点击下方进行查看。</div>
<a href='http://127.0.0.1:8000/{0}/'>查看链接</a>
<div>承办人:WZS_HR</div>
<div>邮件为自动发送,请不要直接回复。</div>
'''.format(x)
return msg
# 定义邮件正文
if webmail_items:
email_content_apply_title = webmail_items.first().email_content_apply_title
email_content_apply_body = webmail_items.first().email_content_apply_body
email_content_agree_title = webmail_items.first().email_content_agree_title
email_content_agree_body = webmail_items.first().email_content_agree_body
email_content_reject_title = webmail_items.first().email_content_reject_title
email_content_reject_body = webmail_items.first().email_content_reject_body
email_content_audit_title = webmail_items.first().email_content_audit_title
email_content_audit_body = webmail_items.first().email_content_audit_body
# email_remark_1 = webmail_items.first().email_remark_1
# email_remark_2 = webmail_items.first().email_remark_2
else:
email_content_apply_title = '【教室借订系统】请您签核教室预定申请'
email_content_apply_body = get_msg1(x='audit_query_booking_apply')
email_content_agree_title = '【教室借订系统】已同意您的教室预定申请'
email_content_agree_body = get_msg2(x='query_booking_apply')
a = '【教室借订系统】已拒绝您的教室预定申请'
email_content_reject_body = get_msg3(x='query_booking_apply')
email_content_audit_title = '【教室借订系统】检查教室使用后的清洁情况'
email_content_audit_body = get_msg4(x='query_booking_apply')
# email_remark_1 = '【教室借订系统】教室使用状态通知'
# email_remark_2 = '【教室借订系统】教室使用状态通知'
def send_exchange_mail(Account1=Account1,Password=Password,primary_smtp_address=primary_smtp_address,subject=subject,body=body,Recipients=Recipients,cc_recipients=cc_list):
# try:
credentials = Credentials(Account1, Password)
account = Account(primary_smtp_address=primary_smtp_address, credentials=credentials, autodiscover=True, access_type=DELEGATE)
print('Recipients',Recipients)
Message1 = Message(
account=account,
subject=subject,
body=HTMLBody(body),
to_recipients=Recipients,
cc_recipients=cc_list,
)
Message1.send()
# except Exception:
# print('发生错误')
# finally:
# pass
@shared_task
def send_apply_email(Account1=Account1,Password=Password,primary_smtp_address=primary_smtp_address,subject=email_content_apply_title,body=body,Recipients=Recipients,cc_recipients=cc_list):
send_exchange_mail(Account1=Account1,Password=Password,primary_smtp_address=primary_smtp_address,subject=email_content_apply_title,body=email_content_apply_body,Recipients=Recipients,cc_recipients=cc_list)
print('send_apply_email')
print('webmail_items.first().hradmin_namelist',webmail_items.first().hradmin_namelist.strip("'").split(';'))
@shared_task
def send_agree_email(Account1=Account1,Password=Password,primary_smtp_address=primary_smtp_address,subject=email_content_agree_title,body=body,Recipients=Recipients,cc_recipients=cc_list):
send_exchange_mail(Account1=Account1,Password=Password,primary_smtp_address=primary_smtp_address,subject=email_content_agree_title,body=email_content_agree_body,Recipients=Recipients,cc_recipients=cc_list)
print('send_agree_email')
@shared_task
def send_reject_email(Account1=Account1,Password=Password,primary_smtp_address=primary_smtp_address,subject=email_content_reject_title,body=body,Recipients=Recipients,cc_recipients=cc_list):
send_exchange_mail(Account1=Account1,Password=Password,primary_smtp_address=primary_smtp_address,subject=email_content_reject_title,body=email_content_reject_body,Recipients=Recipients,cc_recipients=cc_list)
print('send_reject_email')
@shared_task
def send_audit_email(Account1=Account1,Password=Password,primary_smtp_address=primary_smtp_address,subject=email_content_audit_title,body=email_content_audit_body,Recipients=Recipients,cc_recipients=cc_list):
send_exchange_mail(Account1=Account1,Password=Password,primary_smtp_address=primary_smtp_address,subject=email_content_audit_title,body=email_content_audit_body,Recipients=Recipients,cc_recipients=cc_list)
print('send_audit_email')
同步发邮件的代码
views.py
# 邮件
if send_the_mail:
Recipients_x = User.objects.filter(username=booking_employee_id).first().email
Recipients_list = []
Recipients_list.append(Mailbox(email_address=Recipients_x))
print("Recipients_x",Recipients_x)
print("Recipients_list",Recipients_list)
send_apply_email(Recipients=Recipients_list)
else:
pass
return render(request, 'successful.html',locals())
异步发邮件的代码
views.py
# 邮件
if send_the_mail:
Recipients_x = User.objects.filter(username=booking_employee_id).first().email
Recipients_list = []
#Recipients_list.append(Mailbox(email_address=Recipients_x))
Recipients_list.append(Recipients_x) #debug点 #Recipients = ['Season_Zhang@wistron.com']
print("Recipients_x",Recipients_x)
print("Recipients_list",Recipients_list)
#send_apply_email(Recipients=Recipients_list)
send_apply_email.delay(Recipients=Recipients_list)
else:
pass
return render(request, 'successful.html',locals())
celery异步发邮件成功的截图
遇到的坑:Object of type Mailbox is not JSON serializable
解决方法:Recipients_list.append(Mailbox(email_address=Recipients_x))需改为Recipients_list.append(Recipients_x)