0
votes

I've signed up in Microsoft live SMTP server and created an email for my django app to send mail from my domain. But the problem is these configurations doesn't work and I can not send email from my django app running on local host to another email address. What's wrong? This is my code:

#Settings.py
# Email Server config
DEFAULT_FROM_EMAIL = 'Hamid FzM <[email protected]>'
EMAIL_HOST = 'smtp.live.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'something'

For testing purpose each time I visit 127.0.0.1/test/ an email will be send to my account by calling this function

#views
def test(request):
    from django.core.mail import EmailMessage
    EmailMessage('Test', 'This is body', to=['[email protected]'])
1

1 Answers

2
votes

I think you need to load those settings properly and run the send on the message object...

# import settings
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")

# send an email
from django.core.mail import EmailMessage
message = EmailMessage(subject="Peter Maffay", body="test", from_email="[email protected]",  to=["[email protected]"])
message.send(fail_silently=False)

This code fragment works for me