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

atm*_*atm 2 python django smtplib office365

我可以使用此 SO 答案中提供的功能通过 smtplib 发送电子邮件:https : //stackoverflow.com/a/12424439/614770

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?')
Run Code Online (Sandbox Code Playgroud)

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

设置.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'
Run Code Online (Sandbox Code Playgroud)

命令行

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

回溯(最近一次调用最后一次):

文件“manage.py”,第 22 行,在 execute_from_command_line(sys.argv)

文件“C:\Program Files\Anaconda2\envs\django_env\lib\site-packages\django\core\management__init__.py”,第 363 行,在 execute_from_command_line 实用程序.execute()

文件“C:\Program Files\Anaconda2\envs\django_env\lib\site-packages\django\core\management__init__.py”,第355行,在执行self.fetch_command(subcommand).run_from_argv(self.argv)

文件“C:\Program Files\Anaconda2\envs\django_env\lib\site-packages\django\core\management\base.py”,第 283 行,在 run_from_argv self.execute(*args, **cmd_options)

文件“C:\Program Files\Anaconda2\envs\django_env\lib\site-packages\django\core\management\base.py”,第 330 行,在执行输出 = self.handle(*args, **options)

文件“C:\

文件“C:\Program Files\Anaconda2\envs\django_env\lib\site-packages\django\core\mail__init__.py”,第 62 行,在 send_mail 中 return mail.send()

文件“C:\Program Files\Anaconda2\envs\django_env\lib\site-packages\django\core\mail\message.py”,第 348 行,发送返回 self.get_connection(fail_silently).send_messages([self])

文件“C:\Program Files\Anaconda2\envs\django_env\lib\site-packages\django\core\mail\backends\smtp.py”,第 111 行,在 send_messages sent = self._send(message)

文件“C:\ as_bytes(linesep='\r\n'))

文件“C:\Program Files\Anaconda2\envs\django_env\lib\smtplib.py”,第 887 行,在 sendmail 中引发 SMTPDataError(代码,响应)

smtplib.SMTPDataError: (550, b'5.7.60 SMTP; 客户端无权作为此发件人发送 [CY1PR0501MB1116.namprd05.prod.outlook.com]')

我在 Django 中配置错误了吗?

atm*_*atm 9

解决方案是将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'
Run Code Online (Sandbox Code Playgroud)

非常感谢glenfant的有益评论!