AWS SES Setup with Django - django

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

Related

How can I send emails with Django to my gmail account

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.

Send emails with Django and SMTP Google

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.

How to configure Django and G Suite for SMTP

When the user is registering on my website an E-Mail is sent to the user to confirm his/her E-Mail.
It works with this settings:
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'myemail#gmail.com'
EMAIL_HOST_PASSWORD = 'abcdefghiklmnopq'
EMAIL_PORT = 587
Now I want to switch to noreply#mydomain.com.
I created an account on G Suite and made the following configurations:
Comprehensive mail storage (Locally applied) Ensure that a copy of all
sent and received mail is stored in associated users' mailboxes: ON
SMTP relay service (Locally applied) ms_mail Allowed senders: Only
addresses in my domains Only accept mail from the specified IP
addresses: No Require SMTP Authentication: Yes Require TLS encryption:
Yes
Less secure apps (Locally applied) Allow Users to manage their access to less secure apps
Less Secure Apps
Allow less secure apps: ON
Than I created an App Password an tried a lot of configurations like this:
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp-relay.gmail.com'
EMAIL_HOST_USER = 'noreply#mydomain.com'
EMAIL_HOST_PASSWORD = 'abcdefghiklmnopq'
DEFAULT_FROM_EMAIL = 'noreply#mydomain.com'
SERVER_EMAIL = 'noreply#mydomain.com'
EMAIL_PORT = 465
I can't find a good documentation on Google or Django how to configure the settings. Does anybody now a good resource? Is the App Password/Less secure Apps the right way to do it? Because Google has some security warnings. Noreply is a group but I created the app password for my.name#mydomain.com. Is this a problem when I am part of the group? I also tried several options with my.name#mydomain.com instead of noreply#mydomain.com but I always get the error
smtplib.SMTPServerDisconnected: Connection unexpectedly closed
Use this in your settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'noreply#mydomain.com'
EMAIL_HOST_PASSWORD = '#########'
EMAIL_PORT = 587

Authentication Required error when sending email with Django

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

SMTPSenderRefused at /users/password-reset/ (530, b'5.5.1 Authentication Required. Learn more at\n5.5.1 https://support.google.com/ma

I am getting this error while sending mail using django.
/usr/lib/python3.6/smtplib.py in sendmail, line 867
/usr/lib/python3.6/smtplib.py in sendmail
raise SMTPSenderRefused(code, resp, from_addr)
tried mailjet also same issues
If your are using Google SMTP make sure you have the following settings set in your settings.py file:
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'user emailid'
EMAIL_HOST_PASSWORD = 'password'
Also make sure you are logged in with gmail with the provided email id and password in your machine.
Also you need to enable access for less secure apps in your Google account. Here is the link to help you change your Google Account configuration settings link
If you saved your email/password in environ variable for the first time. Please close your terminal and open it again, I hope this will solve your problem. I have run into the same problem and solved issues just like this.
You need to create app password via your google account.
You can't use your email address to send mail.
Go to this link: https://myaccount.google.com/security
For me the error was caused by:
EMAIL_HOST_USER = os.getenv('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = os.getenv('EMAIL_HOST_PASSWORD')
as the env variables where not correctly imported.
Also remember to enable access for less secure apps in your Google account.
Your users must be active and have valid e-mail.
Go here https://myaccount.google.com/security, press on app password and gererate 16-symbolic password and put it to settings.
And use it in settings.py:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'put here your email-address with 16-symbolic password'
EMAIL_HOST_PASSWORD = 'put here your 16-symbols symbolic'