先在 gmail 中设置允许 POP3
在 settings.py 设置
# Email Setup EMAIL_HOST = 'smtp.gmail.com' EMAIL_PORT = 587 EMAIL_HOST_USER = 'your-email@gmail.com' EMAIL_HOST_PASSWORD = 'your email password' EMAIL_USE_TLS = True
发送邮件
from django.core.mail import send_mail
send_mail('Subject here', 'Here is the message.', 'from@example.com',
['to@example.com'], fail_silently=False)
from@example.com 似乎没用,发件人是 your-email@gmail.com['to@example.com'] 是一个列表,可以发送给许多人
可以采用发送 html 邮件,实际上是文本信息附上 html,如果邮件客户端不支持 html,则可以看文本
from django.core.mail import EmailMultiAlternatives
msg = EmailMultiAlternatives('subject', 'text_content', 'from_email', ['to_email'])
msg.attach_alternative('html_content', "text/html")
msg.send()
0 comments:
Post a Comment