Django send_mail not working
Adjust your settings thus:
DEFAULT_FROM_EMAIL = 'workorbit@gmail.com'SERVER_EMAIL = 'workorbit@gmail.com'EMAIL_USE_TLS = TrueEMAIL_HOST = 'smtp.gmail.com'EMAIL_PORT = 587EMAIL_HOST_USER = 'workorbit@gmail.com'EMAIL_HOST_PASSWORD = 'P@ssw0rd5'
Adjust your code:
from django.core.mail import EmailMessagedef send_email(request): msg = EmailMessage('Request Callback', 'Here is the message.', to=['charl@byteorbit.com']) msg.send() return HttpResponseRedirect('/')
If you don't care Preventing header injection:(you should care about it: https://docs.djangoproject.com/es/1.9/topics/email/#preventing-header-injection, but let's continue)
The settings.py
:
EMAIL_HOST = 'smtp.gmail.com'EMAIL_PORT = 587EMAIL_HOST_USER = 'user@gmail.com'EMAIL_HOST_PASSWORD = 'pass'EMAIL_USE_TLS = True
The views.py
(example):
from django.views.generic import Viewfrom django.core.mail import send_mailfrom django.http import HttpResponse, HttpResponseRedirectclass Contacto(View): def post(self, request, *args, **kwargs): data = request.POST name = data.get('name', '') subject = "Thanks %s !" % (name) send_mail(subject, data.get('message', ''), 'user@gmail.com', [data.get('email', '')], fail_silently=False) return HttpResponseRedirect('/')
This is a dangerous way to send an email
When you first try to send the email, you'll receive a google email advising not to do it. You must 'Activate' the 'Less secure apps' (https://www.google.com/settings/security/lesssecureapps) and try again. Second time works.
Google now provides a method to generate a password that you can use for applications that need to relay mail. its different from the password that you would use to login through webmail.
Sign in to Google and start using App Passwords. This allows you to use a 16 digit password to access google services including ability to send out email. Refer below