1.安装库
pip install flask-mail
我没有那么多废话,兄弟们去买菜了,我在写代码。
这次总算没报错
2.配置文件
# smtp 服务的邮箱服务器
EMAIL_HOST = 'smtp.126.com'
# smtp服务固定的端口是25
# EMAL_POST = 25
# 发送邮件的邮箱
EMAIL_HOST_USER = 'landmark_cheng@126.com'
# 在邮箱中设置的客户端授权密码
EMAIL_HOST_PASSWORD = 'land123'
# 收件人看到的发件人<此处要和发送邮件的邮箱相同>
EMAIL_FROM = 'python<landmark_cheng@126.com>'
3.编写代码
def mail_send(request):
mail.send_mail('新冠病毒', '西半夜,uuu', EMAIL_HOST_USER, ['你的邮箱地址'])
return HttpResponse("发送邮件成功")
又开始了
这个是因为需要写自己的邮箱,还有就是
导入库的时候别搞错了
from django.core.mail import send_mail
from django.conf import settings
下面是第二个错误
Traceback (most recent call last):
File "D:\Program Files (x86)\Python38\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "D:\Program Files (x86)\Python38\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Administrator\PycharmProjects\day06\App\views.py", line 22, in mail_send
send_mail('新冠病毒(标题)', '西半夜,uuu(内容)', settings.EMAIL_FROM, ['13960823258@139.com'])
File "D:\Program Files (x86)\Python38\lib\site-packages\django\core\mail\__init__.py", line 61, in send_mail
return mail.send()
File "D:\Program Files (x86)\Python38\lib\site-packages\django\core\mail\message.py", line 284, in send
return self.get_connection(fail_silently).send_messages([self])
File "D:\Program Files (x86)\Python38\lib\site-packages\django\core\mail\backends\smtp.py", line 102, in send_messages
new_conn_created = self.open()
File "D:\Program Files (x86)\Python38\lib\site-packages\django\core\mail\backends\smtp.py", line 69, in open
self.connection.login(self.username, self.password)
File "D:\Program Files (x86)\Python38\lib\smtplib.py", line 734, in login
raise last_exception
File "D:\Program Files (x86)\Python38\lib\smtplib.py", line 723, in login
(code, resp) = self.auth(
File "D:\Program Files (x86)\Python38\lib\smtplib.py", line 646, in auth
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (550, b'\xd3\xc3\xbb\xa7\xce\xde\xc8\xa8\xb5\xc7\xc2\xbd')
[20/Oct/2020 10:40:11] "GET /send/ HTTP/1.1" 500 94925
550, b’\xd3\xc3\xbb\xa7\xce\xde\xc8\xa8\xb5\xc7\xc2\xbd’)
授权密码需要登录自己的126邮箱自己设置
https://jingyan.baidu.com/article/ac6a9a5e13035e2b653eac26.html
不知不觉还没学一个视频又到午饭时间
3.发送多个邮件
from django.core.mail import send_mail, send_mass_mail
def send_many(request):
message1 = ('subject here1', '<b>here is the message22</b>', settings.EMAIL_FROM, ['收件的邮箱 '])
message2 = ('subject here23', '<b>here is the message33</b>', settings.EMAIL_FROM, ['收件的邮箱 '])
send_mass_mail((message1, message2), fail_silently=False)
return HttpResponse("发送邮件成功")
4.渲染魔板进行邮件发送
from django.core.mail import send_mail, send_mass_mail, EmailMultiAlternatives
from django.conf import settings
from django.template import loader
html_content = loader.get_template('active.html').render({'username':'小花猫'})
msg = EmailMultiAlternatives('我是你的苹果', from_email=settings.EMAIL_FROM, to=['邮箱@126.com '])
msg.attach_alternative(html_content, "text/html")
msg.send()