在settings.py里面加
EMAIL_USE_SSL = True
EMAIL_HOST = 'smtp.163.com'
EMAIL_PORT = 465
EMAIL_HOST_USER = 'xxxxxx@163.com'
EMAIL_HOST_PASSWORD = 'xxxxxx'
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
views.py
subject = '一封重要邮件'
list_num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
list_str = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm''n','o', 'p', 'q', 's', 't', 'x', 'y',z']
veri_str = random.sample(list_str, 2)
veri_num = random.sample(list_num, 2)
veri_out = random.sample(veri_num + veri_str, 4)
veri_res = str(veri_out[0]) + str(veri_out[1]) + str(veri_out[2]) + str(veri_out[3])
#mail.py
{{msg}}
#mail.html保存在templates下的邮件模板
template = loader.get_template('mail.html')
html=template.render({"msg":veri_res})
msg = EmailMultiAlternatives(subject, html, settings.EMAIL_HOST_USER, ['18830259@163.com'])
msg.attach_alternative(html, "text/html")
msg.attach_file('/home/lf/pycharm_project/imag.png') # 添加附件发送
msg.send()
# send_mail(subject,msg,settings.EMAIL_HOST_USER,['xxxxxx@163.com'],html_message=html)
return HttpResponse('send ok')