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

时间:2017-08-05 19:35:51

标签: python django office365 smtplib

我可以使用此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?')

但是,当我尝试通过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

     

追踪(最近一次呼叫最后一次):

     

文件“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 utility.execute()

     

文件“C:\ Program Files \ Anaconda2 \ envs \ django_env \ lib \ site-packages \ django \ core \ management__init __。py”,第355行,执行self.fetch_command(子命令).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,**选项)

     

文件“C:\ Program Files \ Anaconda2 \ envs \ django_env \ lib \ site-packages \ django \ core \ management \ commands \ sendtestemail.py”,第33行,在handle recipient_list = kwargs ['email']中,

     

文件“C:\ Program Files \ Anaconda2 \ envs \ django_env \ lib \ site-packages \ django \ core \ mail__init __。py”,第62行,在send_mail中返回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([自])

     

文件“C:\ Program Files \ Anaconda2 \ envs \ django_env \ lib \ site-packages \ django \ core \ mail \ backends \ smtp.py”,第111行,在send_messages中发送= self._send(消息)< / p>      

文件“C:\ Program Files \ Anaconda2 \ envs \ django_env \ lib \ site-packages \ django \ core \ mail \ backends \ smtp.py”,第127行,在_send self.connection.sendmail(from_email,recipients) ,message.as_bytes(linesep ='\ r \ n'))

     

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

     

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

我在django中错误配置了什么吗?

1 个答案:

答案 0 :(得分:3)

解决方案是将 DEFAULT_FROM_EMAIL SERVER_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提供了有用的评论!