0

I'd like to setup an automated mailing system that notifies the Admin user when an Exception has been raised in a Django App. For now, I'd simply like to test the Email notification system and have followed numerous tutorials and tips here and here and here and here, in addition from a few other sites.

I'm using a local Django development environment (not in a live production scenario), with Python 3.5 and Django 1.8. I am on my home network (no proxy involved etc.)

settings.py

ADMINS = (
    ('My Name', 'myhotmailaccount@hotmail.com'),
)
#EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
MAILER_LIST = ['myhotmailaccount@hotmail.com']
EMAIL_HOST = 'smtp.live.com'
EMAIL_HOST_USER = 'myhotmailaccount@hotmail.com'
EMAIL_HOST_PASSWORD = 'myhotmail_password'
EMAIL_PORT = 465
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'noreply@hotmail.com'

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'standard': {
            'format' : "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
            'datefmt' : "%d/%b/%Y %H:%M:%S"
        },
    },
    'handlers': {
        'default': {
            'level':'DEBUG',
            'class':'logging.handlers.RotatingFileHandler',
            'filename': SITE_ROOT + "/logfile.log",
            'maxBytes': 1024*1024*5, #5 MB
            'backupCount': 5,
            'formatter': 'standard',
        },
        'request_handler':{
            'level':'DEBUG',
            'class':'logging.handlers.RotatingFileHandler',
            'filename': SITE_ROOT + "/django_request.log",
            'maxBytes': 1024*1024*5, #5 MB
            'backupCount': 2,
            'formatter': 'standard'
        },
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler',
        }
    },
    'loggers': {
        '': {
            'handlers':['mail_admins', 'default'],
            'level':'DEBUG',
            'propagate': True,
        },
        'django.request': {
            'handlers': ['request_handler'],
            'level': 'DEBUG',
            'propagate': False,
        },
        'django': {
            'handlers': ['request_handler', 'default', 'mail_admins',],
            'propagate': True,
            'level': 'DEBUG',
        },
    }
}

A snippet from a view.py

from django.core.mail import send_mail
from django.core.mail import EmailMessage

def search(request): 
    '''
    other bits of code
    '''     
        send_mail("Subject goes here", "Text goes here", 'noreply@hotmail.com', ['myhotmailaccount@hotmail.com'], fail_silently=True)
        #msg = EmailMessage("Subject goes here", "Text goes here", 'noreply@hotmail.com', ['myhotmailaccount@hotmail.com'])
        #msg.send()
        #return HttpResponse('%s'%res)

The problem is: [Errno 60] Operation timed out. For some reason which I don't quite know what, the Email is not sent. Where am I going wrong?

pymat
  • 1,090
  • 1
  • 23
  • 45
  • A lot of email problems in Django are because the mail provider does not accept the email. In this case Microsoft might not accept email from `noreply@hotmail.com`, an email address that you do not control. – Alasdair Aug 18 '17 at 12:03
  • @Alasdair: thanks but changing this didn't resolve anything. Instead I see the exact same error message. – pymat Aug 18 '17 at 12:57
  • I don't have any other suggestions for using your hotmail email. If you can't get that SMTP server to work, another option is to use [`django-anymail`](https://github.com/anymail/django-anymail) with a provider like mailgun. Many of those providers have free plans which would be enough for personal use. – Alasdair Aug 18 '17 at 13:10
  • @Alasdair: I just figured it out... – pymat Aug 18 '17 at 13:34

1 Answers1

1

The settings were wrongly configured for a hotmail account, which is what I was testing for. Instead of:

EMAIL_HOST = 'smtp.live.com' 
EMAIL_PORT = 465

It should be:

EMAIL_HOST = 'smtp-mail.outlook.com'
EMAIL_PORT = 25

And I tweeked this line (although it makes no difference):

send_mail("hi there", "Text goes here", settings.EMAIL_HOST_USER, ['myhotmailaddress@hotmail.com'], fail_silently=True)
pymat
  • 1,090
  • 1
  • 23
  • 45