pymat
pymat

Reputation: 1192

Testing Django Email for error handling reporting

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', '[email protected]'),
)
#EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
MAILER_LIST = ['[email protected]']
EMAIL_HOST = 'smtp.live.com'
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'myhotmail_password'
EMAIL_PORT = 465
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = '[email protected]'

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", '[email protected]', ['[email protected]'], fail_silently=True)
        #msg = EmailMessage("Subject goes here", "Text goes here", '[email protected]', ['[email protected]'])
        #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?

Upvotes: 0

Views: 1102

Answers (1)

pymat
pymat

Reputation: 1192

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, ['[email protected]'], fail_silently=True)

Upvotes: 1

Related Questions