I have two gsuit emails :
admin#mydomain.com -- this is the main admin
user#mydomain.com
I want to securely send email from user#mydomain.com using python3.7
I created app_password for user#mydomain.com and tried the following code in Django:
settings.py:
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'user#mydomain.com'
EMAIL_HOST_PASSWORD = 'xxxxxxxxxxxxx'
EMAIL_PORT = 587
main function:
message = render_to_string('app1/email.html', {
'fn': ip_dict['first_name'], 'ln': ip_dict['last_name']
})
send_mail(
'hello world',
'',
'user#mydomain.com',
[ip_dict['email']],
html_message=message,
)
I get error :
SMTPAuthenticationError at /url/
(535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials v5sm4332617otk.64 - gsmtp')
With app_password the level of security is low.
With google gmail API https://developers.google.com/gmail/api/quickstart/python the app opens a browser and makes me physically sign in. Is there any other secure way to send an email automatically from gmail .
Thanks
Detailed solution in a Jupyter notebook hosted on Gitlab:
Link
Related
Im having problems with sending emails through gmail in Django. I have set up a app password and yet I cant seem to send emails through Django. My settings.py look like this
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_FROM_USER = 'ianis.donica#gmail.com'
EMAIL_HOST_PASSWORD = 'my app password'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False
To my best of knowledge it isn't a gmail specific issue, as I had experienced the same problems across yahoo mail and Sendgrid, the function that's responsible for sending the email looks like this
def send_activation_email(user, request):
current_site = get_current_site(request)
email_subject = "Activation Email"
context = {"user": user,
"domain": current_site,
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
'token': generate_token.make_token(user)
}
email_body = render_to_string('email/activate.html',context)
email = EmailMessage(subject=email_subject, body=email_body, from_email=settings.EMAIL_FROM_USER, to=[user.email])
email.send()
and the full error message is this
SMTPSenderRefused at /register/
(530, b'5.7.0 Authentication Required. Learn more at\n5.7.0 https://support.google.com/mail/?p=WantAuthError g9-20020a170906394900b00872a726783dsm9975622eje.217 - gsmtp', 'ianis.donica#gmail.com')
What I tried was changing to yahoo and SendGrid mail but the same issues occurred there, just with different names. I also tried changing some details but that shouldn't be the problem? Yet I cant seem to send an email anywhere. If anyone can help me I would really appreciate it
I also have IMAP enabled
The problem was with me using EMAIL_FROM_USER instead of EMAIL_HOST_USER, so I would need to change the code to this
settings.py
...
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'ianis.donica#gmail.com'
EMAIL_HOST_PASSWORD = 'my app password'
...
views.py
...
email = EmailMessage(subject=email_subject, body=email_body, from_email=settings.EMAIL_HOST_USER, to=[user.email])
...
This is because without EMAIL_HOST_USER, Django won't try to authenticate
I am trying to request a reset password from a system deployed on heroku but i get this error.
SMTPAuthenticationError at /password-reset/
(534, b'5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbs\n5.7.14 i1cE5v2I-3UUFLl6jQMnBfbcvZuRiGo_q9k1T5h2XgMGxtKYIJ0sIPE9Jr6zW1X78xQM3\n5.7.14 Py7mrOXSPkPG01upxjbZRi68aLrKIbvG_4uHrli9l7ZVmFnr8CxRNb9JUDGVcm8M>\n5.7.14 Please log in via your web browser and then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/answer/78754 k188sm1479373qkd.98 - gsmtp')
I have set all these settings and they seem to be okay but i still get the same error. Kindly help
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = os.environ.get('EMAIL_USER')
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_PASS')
Your application is probably fine, but the issue is with Google, that is preventing your login as it is a potentially suspicious activity. You may have received a mail from Google telling you that a suspicious activity has happened.
To solve this problem, you have to change your Google's security settings and create an app password so you can login from your application:
Using a browser, login to your Gmail account
Go to "Manage your account"
Go to "Security"
In "Signing to Google" section, click on "App passwords"
Enter your password
In "Select the app and device you want to generate the app password for.", select Other, name it anyway you want and click "Generate"
Use this password in Django instead of your regular password.
I am trying to send email using my django app. But after setting Zoho account and adding necessary lines in settings.py I am still not able to send email and it keep giving SMTPAuthenticationError (535, b'Authentication Failed').
#settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.zoho.com'
EMAIL_HOST_USER = 'administrator#technovate-iiitnr.org'
EMAIL_HOST_PASSWORD = 'mypass'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
#views.py
html = render_to_string('email/code_email.html',{'code':code})
send_mail('Your Code',
'Hello',
'administrator#technovate-iiitnr.org',
['example#gmail.com'],
html_message=html
)
return render(request,'index.html')
One of the reason could be 2-factor authentication being used in the zoho account.
This prevents you from using account password, instead you need to generate application specific password and use the same.
Read more here
I'm working on a project hosted on Google App Engine, and using Django-allauth for my user system.
Right now I'm just using the following setup in settings.py
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = DEFAULT_FROM_EMAIL = 'myMail#gmail.com'
EMAIL_HOST_PASSWORD = 'password'
But I would like to use GAE's Mail API instead, so that I can take use of all the quotas available.
To send an email with GAE's API I can do as follows:
sender_address = "myMail#gmail.com"
subject = "Subject"
body = "Body."
user_address = "user#gmail.com"
mail.send_mail(sender_address, user_address, subject, body)
As I understand it from the allauth documentation, I can "hook up your own custom mechanism by overriding the send_mail method of the account adapter (allauth.account.adapter.DefaultAccountAdapter)."
But I'm really confusing about how to go about doing this.
Does it matter where I place the overridden function?
Any additional tips would be greatly appreciated.
My Solution
What I did to get Django-allauth email system to work with Google App Engine mail API
Created a file auth.py in my 'Home' app:
from allauth.account.adapter import DefaultAccountAdapter
from google.appengine.api import mail
class MyAccountAdapter(DefaultAccountAdapter):
def send_mail(self, template_prefix, email, context):
msg = self.render_mail(template_prefix, email, context)
sender_address = "myEmailAddress#gmail.com"
subject = msg.subject
body = msg.body
user_address = email
mail.send_mail(sender_address, user_address, subject, body)
In order to use your email as sender with GAE's mail API, it is important to remember to authorize the email as a sender
Lastly, as e4c5 pointed out, allauth has to know that this override exists, which is done as so in settings.py
ACCOUNT_ADAPTER = 'home.auth.MyAccountAdapter'
You have to tell django-allauth about your custom adapter by adding the following line to settings.py
ACCOUNT_ADAPTER = 'my_app.MyAccountAdapter'
taking care to replace my_app with the correct name
I'm trying to send an email through Django's wrapper.
Here are my relevant settings.
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'myemail#gmail.com'
EMAIL_HOST_PASSWORD = 'mypassword'
EMAIL_PORT = 587
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
The Email, which I am trying to isolate in the most basic form of a view:
from django.core.mail import send_mail
def index(request):
subject = 'Subject'
message = 'message'
from_email = settings.DEFAULT_FROM_EMAIL
send_mail(subject, message, from_email, ['email#example.com'])
return render(request, "index.html")
All of the emails and password are legitimate. When I execute the code, I get thrown an error message:
SMTPAuthenticationError at /....*Link to sign into my account*
Please log in via your web browser and\n5.7.14 then try again
I did that but continue to get the same message. The password I give in the app is correct. Is there anything I need to configure in my gmail account?
Change EMAIL_HOST = 'smtp.gmail.com ' toEMAIL_HOST = 'smtp.gmail.com' and I bet your problem will disappear :)
EDIT #1
You are running into authentication issues as EMAIL_USE_TLS is True and Gmail only requires TLS connections for SMTP on port 587. Change to EMAIL_PORT = 587 and you should bypass the issue.
EDIT #2
The error you see can be fixed through your Gmail settings. See -
Django SMTPAuthenticationError