Search code examples
pythondjangooffice365smtplib

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


I am able to send emails via smtplib by using the function provided in this SO answer: 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?')

However, I receive the following error when I try to send email via 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'

Command line

$ 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]')

Have I misconfigured something in django?


Solution

  • The solution was to add DEFAULT_FROM_EMAIL and SERVER_EMAIL to 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'
    

    Thank you very much to glenfant for the helpful comments!