I need to use google's SMTP mail server for my server to send e-mails to x recipients. But the mail never reaches the intended recipient. I have activated the non-secure applications in my gmail account and configured the following:
In Gmail I go to "Manage your GMAIL account" -> "Security" and activate 2-step verification. Then in "Manage your GMAIL account" -> "Security" -> "Application password" in selecting application I choose "Other (custom name)" and I add a name for the application and it generates a password of 16-digit characters.
Now the Django code is as follows:
from django.conf import settings
from django.core.mail import EmailMultiAlternatives
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'xxxxxxxxx#gmail.com'
EMAIL_HOST_PASSWORD = 'xxxx xxxx xxxx xxxx' #contrasena de 16 digitos
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'default from email'
def send_user_mail(randomNumber):
subject = 'Titulo del correo'
message = EmailMultiAlternatives(subject, #Titulo
"Su nĂºmero es: "+str(randomNumber),
'xxxxx#gmail.com', #Remitente
to=['xxxx#gmail.com']) #Destinatario
message.send()
in settings.py I added:
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
Am I forgetting something or doing something wrong? Thanks in advance.
Related
I've tried sending emails with my Gmail account in Django, but I always get errors. I tried using a sandbox (Mailtrap) and it worked but using the actual Gmail account doesn't. I've also tried using App Password in my google account but it still doesn't work.
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = "smtp.gmail.com"
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = "example#gmail.com"
EMAIL_HOST_PASSWORD = "password"
Error I always get;
SMTPConnectError at /send/
(421, b'Service not available')
Please how can this be fixed.
I've done it recently using Google App Password. Follow instructions to get 2-step verification, get the password and then put it in your EMAIL_HOST_PASSWORD enviromental variable with quotes. (EMAIL_HOST_PASSWORD = 'xxxxxxxxxxxxxxxxxx'). The other parameters I've set are as follows:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = config('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = config('EMAIL_HOST_PASSWORD')
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False
Finally I send the emails on my view with send_mail
from django.core.mail import send_mail
You have serval options.
Apps password smtp
Does the google account you are using have 2fa enabled, if it does then you can create an apps password. Then take the apps password and use that implace of the password in your code.
XOauth2 smtp
Depending upon the system you are using to send emails it may support Xoauth2 authorization. If it does then you can use that to send emails via the smtp server.
Gmail api. Oauth2 or service accounts
You can switch to using the gmail api, if you have a workspace account then you could use service accounts. If not then you will need to stick with Oauth2 and authorize your appliaton once. This is simarl to using Xoauth2 with the smtp server only you will be going though the gmail api instead.
How to create a Apps Password for connecting to Google's SMTP server.
I am trying to add AWS SES into my project. Where I get a different region listed
MessageRejected at /
An error occurred (MessageRejected) when calling the SendRawEmail operation: Email address is not verified. The following identities failed the check in region US-EAST-1: email#hotmail.com
In the AWS verified identities it is listed:
email#hotmail.com Email address Verified
I have the permissions for the SES for my policy for that user.
Attached from group
AmazonSESFullAccess
AWS managed policy from group AmazonSESFullAccess
Also my settings.py
EMAIL_BACKEND = 'django_ses.SESBackend'
EMAIL_HOST = 'email-smtp.us-west-2.amazonaws.com'
EMAIL_PORT = 465
EMAIL_USE_SSL = True
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
Where it's used
from django.core.mail import send_mail
from django.conf import settings
send_mail(email_subject, email_message,'email#hotmail.com',['email#hotmail.com'])
1. Amazon SES sandbox
I'm assuming you're in Sandbox mode, which means you need to add the recipients' email addresses to successfully send the email. You can make a request to exit sandbox mode, then the daily limit of sent emails will increase and you will not have to define them.
More on this topic can be found here:
https://docs.aws.amazon.com/ses/latest/dg/request-production-access.html
2. New settings with TLS
The second option is to check if the mail works with TLC settings:
Example settings:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'email-smtp.<your AWS region>.amazonaws.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'smtp_username'. # IAM user SMTP Credentials
EMAIL_HOST_PASSWORD = 'smtp_password' # IAM user SMTP Credentials
DEFAULT_FROM_EMAIL = 'my#mail.com' # You need to verify this mail
I'm trying to send a password-reset email using gmail server but I'am getting an error. 'SMTPSenderRefused Authentication Required'
The google account have - 1. 2-Step Verification on 2. Third-party access through app password 3. Access allowed for less secure app : On
'''
setting.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = os.environ.get('EMAIL_HOST_USER') #my gmail acc
EMAIL_PASSWORD = os.environ.get('EMAIL_PASSWORD') #app password from gmail acc
EMAIL_PORT = 587
'''
'''
urls.py
path('password-reset/', PasswordResetView.as_view(
template_name='users/password-reset.html'), name='password_reset'),
path('password-reset-done/', PasswordResetDoneView.as_view(
template_name='users/password-reset-done.html'),
name='password_reset_done'),
path('password-reset-confirm/<uidb64>/<token>/',
PasswordResetConfirmView.as_view(
template_name='users/password_reset_confirm.html'),
name='password_reset_confirm'),
'''
I expect an email with a reset-password-link send to the user or whomever POSTed their email when prompted but what i get is this error -
'''
SMTPSenderRefused at /password-reset/
(530, b'5.5.1 Authentication Required. Learn more at\n5.5.1
https://support.google.com/mail/?p=WantAuthError p17sm3671371wrq.95 -
gsmtp', 'webmaster#localhost')
'''
create a new account in google and avoid two step verification.
Add your account name and password to settings directly
It will work
As you are new, hardcore these details directly to settings.py later you can configure in local environment
The following works locally but as I deployed it to production hosting at Digital Ocean, the email is not sending as I test on shell command (python manage.py shell) like below. The send_mail line just got stuck there and am getting error: [Errno 101] Network is unreachable after few minutes. How can I capture the error on the email sending? Please advise how can I troubleshoot this issue.
from django.core.mail import send_mail
send_mail('test email', 'hello world', 'xxxx#gmail.com', ['xxxx#gmail.com'],fail_silently=False)
# Email settings
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_PASSWORD = 'xxxx' #my gmail password
EMAIL_HOST_USER = 'xxxx#gmail.com' #my gmail username
EMAIL_PORT = 587
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
Your Django app settings look correct but you should also head over to https://accounts.google.com/DisplayUnlockCaptcha and make sure you enable access to lower security applications.
Also consider creating an application-specific password if you're using Two-Factor-Authentication.
I build a mail sending function in django.I did it by referring django Doc .
In doc to specify a email backend,need to add this line in settings.py
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
settings.py for email
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'anjunair#gmail.com'
EMAIL_HOST_PASSWORD = '*********'
DEFAULT_FROM_EMAIL = 'quartentine#name.com'
Mail is sending to the email if the EMAIL_BACKEND removed from settings.Need some clarification about this.
Thanks
The docs don't tell you to do that. That console email backend is specifically to NOT use SMTP - it outputs directly to the console.
Remove it, and keep it that way if you want to send via smtp (gmail).