def email_template(book_id,borrow_book,yg_name): html = ''' <p>详情</p> <table border="1" cellpadding="0" cellspacing="0" width="1800" style="border-collapse: collapse;"> <tbody> <tr> <th>编号</th> <th>名称</th> <th>员工</th> <th>管理系统地址</th> </tr> <tr> <td style="color:red;text-align:center"> {0} </td> <td style="text-align:center"> {1} </td> <td style="text-align:center"> {2} </td> <td style="text-align:center"> <a href="http://10.153.46.214:8000/" target="_blank">管理系统</a> </td> </tr> </tbody> </table> ''' html = html.format(id,name,yg_name) return html
#封装邮件发送方法 from django.core.mail import send_mail
def send_email(subject,message,html_message,email_user): send_mail( subject=subject, message=message,#如果有html_message,则该项被html_message覆盖,但是为必填参数。 html_message=html_message,#html格式的message from_email='xxxx@qq.com', # 发件人 recipient_list=[email_user], # 收件人 #收件人可以直接写,也可以从setting.py中配置中导入 fail_silently=False ) print("邮件发送成功") return "邮件已发送"
#可以封装为方法 subject = "提醒" message = "xxx" html_message = email_template(id,xxx,yg_name) send_email(subject,message,html_message,email_user)
邮件截图:
Stack Overflow类似的问题:https://stackoverflow.com/questions/38275467/send-table-as-an-email-body-not-attachment-in-python