2

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?

atm
  • 1,684
  • 1
  • 22
  • 24
  • The error message is explicit. Try to use real sender / recipient addresses. – glenfant Aug 05 '17 at 20:04
  • I just replaced my actual email address the dummy one for this post. – atm Aug 05 '17 at 22:33
  • 1
    Hints : (1) using the "-v" option (verbosity) will add valuable information. Try: python manage.py sendtestemail -v 3 my@email.com (2) try the console backend and have a look at the built mail body + headers : https://docs.djangoproject.com/en/1.11/topics/email/#console-backend (3) Try with a debugging server, the one that's provided by Python https://docs.djangoproject.com/en/1.11/topics/email/#configuring-email-for-development or (my favorite) Mailcatcher https://mailcatcher.me/ – glenfant Aug 06 '17 at 10:40
  • I updated the original post with the verbose error message. I also tried setting EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' in settings.py and it was successful. – atm Aug 06 '17 at 16:58
  • 2
    Okay I think I understand. What is the "From" header in the mail that show when using the console backend ? Is this mail address known from hour `EMAIL_HOST` smtp server ? – glenfant Aug 07 '17 at 00:45

1 Answers1

9

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!

atm
  • 1,684
  • 1
  • 22
  • 24