2017-08-05 55 views
1

我能夠通過使用該提供的功能來發送通過的smtplib郵件SO回答:https://stackoverflow.com/a/12424439/614770Django的電子郵件:客戶端沒有權限發送的該發件人

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(
     '[email protected]', 'password', '[email protected]', 
     '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 = '[email protected]' 
EMAIL_HOST_PASSWORD = 'password' 

命令線

$ python manage.py sendtestemail -v 3 [email protected]

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的東西嗎?

+0

錯誤消息是顯式的。嘗試使用真實的發件人/收件人地址。 – glenfant

+0

我剛剛取代了我的真實電子郵件地址這個職位的假人。 – atm

+1

提示: (1)使用「-v」選項(詳細)將增加有價值的信息。嘗試: python manage.py sendtestemail -v 3 [email protected] (2)嘗試控制檯後端並查看構建的郵件正文+標題:https://docs.djangoproject.com/en/ 1.11/topics/email /#console-backend (3)嘗試使用一個由Python提供的調試服務器https://docs.djangoproject.com/en/1.11/topics/email/#configuring-email- for-development或(我最喜歡的)Mailcatcher https://mailcatcher.me/ – glenfant

回答

2

解決的辦法是增加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了有益的意見!

+1

這就是我想要驅動你的結論;) – glenfant

相關問題