在django中配置使用gmail来发送邮件,只要在setting.py文件中加入
EMAIL_HOST = 'smtp.gmail.com'EMAIL_PORT = '25'EMAIL_HOST_USER = 'username' #加username@gmail.com也可以。EMAIL_HOST_PASSWORD = 'password'EMAIL_USE_TLS = True 测试:
python manage.py shellIn [1]: from django.core.mail import send_mailIn [2]: send_mail('Subject', 'Body of the message.', 'from_username@gmail.com',['to_username@gmail.com'])Out[2]: 1#输出一表示成功
ps: 改过setting中的参数需要exit()退出shell,再次python manage.py shell进入。
发送html邮件
from django.core.mail import EmailMessagefrom django.template import loaderfrom settings import EMAIL_HOST_USERdef send_html_mail(subject, html_content, recipient_list): msg = EmailMessage(subject, html_content, EMAIL_HOST_USER, recipient_list) msg.content_subtype = "html" # Main content is now text/html msg.send()