错误示范:
from django.core.mail import send_mail
send_mail(subject="subject",
message="message",
from_email='EMAIL_HOST_USER',
recipient_list=['EMAIL_RECIPIENT'])
报错:
[Errno 99] Cannot assign requested address
问题原因:
看send_email源码:
def send_mail(subject, message, from_email, recipient_list,
fail_silently=False, auth_user=None, auth_password=None,
connection=None, html_message=None):
"""
Easy wrapper for sending a single message to a recipient list. All members
of the recipient list will see the other recipients in the 'To' field.
If auth_user is None, use the EMAIL_HOST_USER setting.
If auth_password is None, use the EMAIL_HOST_PASSWORD setting.
Note: The API for this method is frozen. New code wanting to extend the
functionality should use the EmailMessage class directly.
"""
connection = connection or get_connection(
username=auth_user,
password=auth_password,
fail_silently=fail_silently,
)
mail = EmailMultiAlternatives(subject, message, from_email, recipient_list, connection=connection)
if html_message:
mail.attach_alternative(html_message, 'text/html')
return mail.send()
注意注释:
If auth_user is None, use the EMAIL_HOST_USER setting.
If auth_password is None, use the EMAIL_HOST_PASSWORD setting.
如果settings.py没有配置EMAIL_HOST_USER和 EMAIL_HOST_PASSWORD,需要手动传入auth_user和auth_password.
from django.core.mail import send_mail
send_mail(subject="subject",
message="message",
from_email='EMAIL_HOST_USER',
recipient_list=['EMAIL_RECIPIENT'],
auth_user='zhangsan',
auth_password='ew33ddds')