【Django-Meeting系统】公司邮箱的邮件通知功能-20220717

景品
2023-12-01

每次改完密码,记得重启pod!
每次改完密码,记得重启pod!
每次改完密码,记得重启pod!

效果:启动公司邮箱来发邮件

代码

  • ExchangeEmailSent.py
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 = ['Season_Zhang@wistron.com']
cc_list = ['Season_Zhang@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 = 'wzscn\Z18093014'
    Password = 'Huang1013@'
    primary_smtp_address='Shirleen_Huang@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
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')
    email_content_reject_title = '【教室借订系统】已拒绝您的教室预定申请'
    email_content_reject_body = get_msg3(x='query_booking_apply')
    email_content_audit_title = '【教室借订系统】检查教室使用后的清洁情况'
    email_content_audit_body = get_msg4(x='query_booking_apply')



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


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(';'))

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')

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')

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


# 提交审核意见
@login_required
def booking_audit(request):
    '''管理员填写教室5S检查结果'''

    # 验证权限
    hr_permission = request.user.has_perm('myclassroom.add_classpermission')

    # 提交查询条件
    booking_all = booking.objects.all()
    if ('booking_id' in request.POST and request.POST['booking_id'] ) and \
        ('booking_audit' in request.POST and request.POST['booking_audit'] ) and \
        ('booking_audit_description' in request.POST and request.POST['booking_audit_description'] ):

        # 获取POST数据
        booking_id = request.POST.get('booking_id', '')
        booking_audit = request.POST.get('booking_audit', '')
        booking_audit_description = request.POST.get('booking_audit_description', '')
        booking_employee_id = booking_all.filter(booking_id=booking_id).first().booking_employee_id

        
        '''更新booking模型'''
        '''更新classroomschedule模型'''
        # 如果可以申请,则更新booking模型
        if 1:
            booking_items = booking_all.filter(booking_id=booking_id).\
            update(
            booking_audit=booking_audit,
            booking_audit_description=booking_audit_description,)
            # 邮件
            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))
                send_audit_email(Recipients=Recipients_list)
            else:
                pass
            
            return render(request, 'successful.html',locals())
        else:
            return HttpResponse("未更新成功,点击<a href='/edit_audit_query_booking_apply_result/{0}/'>此处</a>返回".format(booking_id))

        

    # 返回空白表单
    else:
        return HttpResponse("无数据,提交失败,点击<a href='/edit_audit_query_booking_apply_result/{0}/'>此处</a>返回".format(booking_id))


 类似资料: