Django with smtp.gmail.com with secured google apps account - django

Django websites were working 100% with contact form that sends email via google apps. Recently it stopped working that I need to turn on this feature:
Allowing less secure apps to access your account
Link:
https://support.google.com/accounts/answer/6010255?hl=en
How to achieve to send emails via contact form using google apps but secured way. I do not want my google apps account to be less secure only because of this contact form. Is there a way to set an exception in google apps account for this website / IP or somehow? I would like to have it set proper way.
This setting worked previously till now when I had to turn on less secure apps feature on.
RECAPTCHA_PUBLIC_KEY = env("RECAPTCHA_PUBLIC_KEY", default='XXX')
RECAPTCHA_PRIVATE_KEY = env("XXX", default='RECAPTCHA_PRIVATE_KEY')
CONTACT_EMAIL_SUBJECT = 'CUSTOM SUBJECT'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'EMAIL ADDRESS'
EMAIL_HOST_PASSWORD = 'EMAIL PASSWORD'
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'EMAIL ADDRESS'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

Related

Sending emails in Django with NameCheap email account

I set up an email account with NameCheap that I use with my Django app. Below is my email configuration based on information provided by NameCheap
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'mail.privateemail.com'
EMAIL_PORT = 465
EMAIL_HOST_USER = 'news#mydomain.com'
EMAIL_HOST_PASSWORD = os.getenv('EMAIL_PASSWORD')
EMAIL_USE_SSL = True
DEFAULT_FROM_EMAIL = 'WebsiteTitle <news#mydomain.com>'
Now when I use send_mail() django function (eg. after registration) it is like playing roulette - one time it works properly and sends out an email, another time it throws an error saying
SMTPServerDisconnected at /reset-password/
Connection unexpectedly closed
As if something was wrong with my email configuration.
Do you have any idea what might cause it or how to try to fix it or debug it? Is it more likely NameCheap or Django?

Django contact form: email not sending

I'm currently building a contact form for my website in Django.
The only problem is that the email is not being sent when the form is submitted.
This is the code:
settings.py:
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = "c******o#gmail.com"
EMAIL_HOST_PASSWORD = '****'
EMAIL_PORT = '587'
views.py:
sender_name = form.cleaned_data['nome_completo']
sender_email = form.cleaned_data['email']
message = "{0} has sent you a new message:\n\n{1}".format(sender_name, form.cleaned_data['messaggio'])
send_mail('New Enquiry', message, sender_email, ['c******o#gmail.com'])
My thought:
Since I'm in a virtualenv, when I submit the form, the terminal displays that it was successfully submitted, and gets me back the code right, maybe the email is not sent because of that, or maybe because I'm using a gmail account? Thanks!
Since you are using an SMTP server, why not use the following backend:
django.core.mail.backends.smtp.EmailBackend
Also, if you are using Gmail, make sure of the following:
Two-Factor authentication is enabled on account
You are using an app password.
You can find these on the security tab on your google account page:
https://myaccount.google.com/security
So the issue is with EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
This outputs emails to your console for debugging instead of actually sending emails. (See https://docs.djangoproject.com/en/3.1/topics/email/#console-backend)
Don't set EMAIL_BACKEND to anything for production as it defaults to django.core.mail.backends.smtp.EmailBackend. Then your EMAIL_HOST settings will take effect and the email should be sent out.

Django -Email is appearing in console instead of being sent to recipient

I am using a business email for my website. Website has two parts, One is "Contact Us" form where email will be sent to my [business email]. Second part requires email to be sent from [business email] to customers emails.
I have set up django mail as per documentation:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'cp1.mywebsitebox.com'
EMAIL_HOST_USER = '*********'
EMAIL_HOST_PASSWORD = '********'
EMAIL_PORT = 26
DEFAULT_FROM_EMAIL = '*******'
Issue is that, first part goes well. Email recieves successfully at [business email] but second part is totally not working. Instead of being sent to recipient, it appears only in console.
Can anyone suggest what the issue is?
it may be issue from your mailing server, can you try gmail server for testing
EMAIL_BACKEND = ‘django.core.mail.backends.smtp.EmailBackend’
EMAIL_HOST = ‘smtp.gmail.com’
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = ‘your_account#gmail.com’
EMAIL_HOST_PASSWORD = ‘your account’s password’
and enable less secure app for testing
https://myaccount.google.com/lesssecureapps

How to send emails from Gmail without turning on "allow less secure apps" setting?

Is there any way to send an email in a Django project without turning on the "allow less secure apps" feature? Is using OAuth2 a must or can I send it using Google's API?
So far, I just use:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = '587'
EMAIL_HOST_USER = 'myemailaddress'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False
But this gives me an SMTPAuthenticationError, which only works when I turn on "allow less secure apps" in Google. Is there any way to circumvent this without using OAuth2 and allow multiple users to login to that account easily?
Using the gmail app password through you can send the email not provide the actual password and no one can login in your account. Without enable less secure apps setting here is give the link try and it's work let me know if it right google or gmail app password
after it change the actual password to your app password in
setting.py file EMAIL_HOST_PASSWORD = 'your app password' give by google
not change in other constant
if you do not use 2 step email verification you need to use third party service here i give the link you see sendgrid

How can I change email sender's name in django

I'm creating email system in my django project and faced to next question:
Send email from custom email sender name, f.e. sending email from gmail(btw it works in my project) requires sending messages only from gmail account, same thing with smtp-pulse, sendgrid and etc. .
My question is: May I use any of already configured smtp servers in my case, or I only need to create my own using smtplib(f.e) and configure it?
Tried:
DEFAULT_FROM_EMAIL = 'support#site.com'
SERVER_EMAIL = 'support#site.com'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 2525
EMAIL_USE_TLS = False
EMAIL_HOST_USER = 'user#gmail.com'
EMAIL_HOST_PASSWORD = 'password'
This one doesn't work.
Same with mail.ru/smtp-pulse.com
Try
DEFAULT_FROM_EMAIL = "Site Support <support#site.com>"