Django 电子邮件:客户端无权作为此发件人发送

Django email: Client does not have permission to send as this sender

我可以使用此 SO 答案中提供的功能通过 smtplib 发送电子邮件:

from __future__ import print_function

def send_email(user, pwd, recipient, subject, body):
    import smtplib

    FROM = user
    TO = recipient if type(recipient) is list else [recipient]
    SUBJECT = subject
    TEXT = body

    # Prepare actual message
    message = """From: %s\nTo: %s\nSubject: %s\n\n%s
    """ % (FROM, ', '.join(TO), SUBJECT, TEXT)
    try:
        server = smtplib.SMTP('smtp.office365.com', 587)
        server.ehlo()
        server.starttls()
        server.login(user, pwd)
        server.sendmail(FROM, TO, message)
        server.close()
        print('Successfully sent the mail')
    except:
        print('Failed to send mail')


if __name__ == '__main__':
    send_email(
        'my@email.com', 'password', 'my@email.com', 
        'Test Email', 'Can you see this?')

但是,当我尝试通过 django 发送电子邮件时收到以下错误:

settings.py

# Email
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.office365.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'my@email.com'
EMAIL_HOST_PASSWORD = 'password'

命令行

$ python manage.py sendtestemail -v 3 my@email.com

Traceback (most recent call last):

File "manage.py", line 22, in execute_from_command_line(sys.argv)

File "C:\Program Files\Anaconda2\envs\django_env\lib\site-packages\django\core\management__init__.py", line 363, in execute_from_command_line utility.execute()

File "C:\Program Files\Anaconda2\envs\django_env\lib\site-packages\django\core\management__init__.py", line 355, in execute self.fetch_command(subcommand).run_from_argv(self.argv)

File "C:\Program Files\Anaconda2\envs\django_env\lib\site-packages\django\core\management\base.py", line 283, in run_from_argv self.execute(*args, **cmd_options)

File "C:\Program Files\Anaconda2\envs\django_env\lib\site-packages\django\core\management\base.py", line 330, in execute output = self.handle(*args, **options)

File "C:\Program Files\Anaconda2\envs\django_env\lib\site-packages\django\core\management\commands\sendtestemail.py", line 33, in handle recipient_list=kwargs['email'],

File "C:\Program Files\Anaconda2\envs\django_env\lib\site-packages\django\core\mail__init__.py", line 62, in send_mail return mail.send()

File "C:\Program Files\Anaconda2\envs\django_env\lib\site-packages\django\core\mail\message.py", line 348, in send return self.get_connection(fail_silently).send_messages([self])

File "C:\Program Files\Anaconda2\envs\django_env\lib\site-packages\django\core\mail\backends\smtp.py", line 111, in send_messages sent = self._send(message)

File "C:\Program Files\Anaconda2\envs\django_env\lib\site-packages\django\core\mail\backends\smtp.py", line 127, in _send self.connection.sendmail(from_email, recipients, message.as_bytes(linesep='\r\n'))

File "C:\Program Files\Anaconda2\envs\django_env\lib\smtplib.py", line 887, in sendmail raise SMTPDataError(code, resp)

smtplib.SMTPDataError: (550, b'5.7.60 SMTP; Client does not have permissions to send as this sender [CY1PR0501MB1116.namprd05.prod.outlook.com]')

我在 django 中配置错误了吗?

解决方案是将DEFAULT_FROM_EMAILSERVER_EMAIL添加到settings.py:

# Email
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.office365.com'
EMAIL_HOST_USER = '****@*******.com'
EMAIL_HOST_PASSWORD = '**********'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = '****@*******.com'
SERVER_EMAIL = '****@*******.com'

非常感谢glenfant的宝贵意见!