How to setup flask mail with zoho mail - python

I have tried what I can think of but can't get the email to be sent from my application using flask-mail with Zoho mail.
I've tried setting up an app password and I have tried the following examples of configuration using some of the information from their site:
https://www.zoho.com/mail/help/pop-access.html
app.config['MAIL_SERVER'] = 'smtp.zoho.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USE_SSL'] = True
app.config['MAIL_USERNAME'] = '_#whatever.com'
app.config['MAIL_PASSWORD'] = 'XXXXXXXXXXXX'
app.config['MAIL_SERVER'] = 'smtp.zoho.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = '__#whatever.com'
app.config['MAIL_PASSWORD'] = 'XXXXXXXXXXX'
I would expect to be able to send an email using flask-mail with my custom domain which is setup with zoho.

Check which host of Zoho you are registered with, if you are based in Europe you will have to edit the MAIL_SEVER parameter to:
app.config['MAIL_SERVER'] = 'smtp.zoho.eu'
This should solve the SMTPAuthenticationError.
Similar question link

For secure access head on to general setting on Zoho mail(https://mail.zoho.com/zm/#settings/general)
My account>Security Questions
Application-Specific Passwords > Generate new password
name your app. It'll give you a password
export MAIL_PASSWORD=<generated-password>
then in config.py
import os
MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD')
And when you use TLS Zoho on time of writing this uses the port 587\

Related

Using Sendgrid and Azure : 550, b'Unauthenticated senders not allowed'

I am trying to send a password reset email to users, via an App we are developping with Django.
When trying the App locally, the user can select to reset a pwd if forgotten , input his email into a field and submit, in order to receive an email. The sender email is a business email address.
Checking into Sendgrid, I can see the activity log that the email has been processed and delivered. So it seems working.
However, when trying to do the same passing via Github, Azure, on https://XXXXXX.azurewebsites.net/en/password_reset/, I get the following :
SMTPSenderRefused at /en/password_reset/
(550, b'Unauthenticated senders not allowed', 'nicolas#XXXXX.com')
in the log I get the following as well:
raise SMTPSenderRefused(code, resp, from_addr)
Is there something I am missing with Azure, another key to include. I read through many similar issues, but could not find a suitable response to my problem. The fact Sendgrid works locally but not with Azure make me think I am missing a connection at that level. Otherwise, All other aspects of the App works when hosting it on Azure..
Below are the codes I am using:
in settings.py
import os
ALLOWED_HOSTS = ['XXXXXX.azurewebsites.net']
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
SENDGRID_API_KEY = os.getenv('SENDGRID_API_KEY')
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey' #Exactly that
EMAIL_HOST_PASSWORD = SENDGRID_API_KEY
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'nicolas#XXXXX.com'
in views :
from django.core import mail
from django.template.loader import render_to_string
from django.utils.html import strip_tags
def send_password_reset_email(request):
subject = 'email reset'
html_message = render_to_string('password_reset_email.html', {'context': 'values'})
plain_message = strip_tags(html_message)
from_email = 'nicolas#XXXXX.com'
form = (request.POST)
if form.is_valid():
data = form.cleaned_data
to=data.get("email")
mail.send_mail(subject, plain_message, from_email, [to], html_message=html_message,fail_silently=False)
in url.py:
urlpatterns=[
url('', views.send_password_reset_email)
]
merci
Nicolas
This is solved: If SendGrid works locally, but not on Azure:
In Azure, select ‘configuration', 'Application settings', 'new application settings' and then, as per below, using the correct API name made it work.
In SendGrid, I had named my API key ‘test’, and thought in Azure I should name it the same. However, I should have named it the same as in my code:
SENDGRID_API_KEY
Not obvious at first.

flask_mail failed to send email [duplicate]

When I try to send an email using Flask-Mail to Gmail's SMTP server using the settings below, I get [Errno -2] Name or service not known. How do I fix my configuration to send email with Gmail?
from flask import Flask, render_template, redirect, url_for
from flask_mail import Mail, Message
app = Flask(__name__)
app.config.update(
MAIL_SERVER='smtp#gmail.com',
MAIL_PORT=587,
MAIL_USE_SSL=True,
MAIL_USERNAME = 'ri******a#gmail.com',
MAIL_PASSWORD = 'Ma*****fe'
)
mail = Mail(app)
#app.route('/send-mail/')
def send_mail():
msg = mail.send_message(
'Send Mail tutorial!',
sender='ri******a#gmail.com',
recipients=['ri*********07#msn.com'],
body="Congratulations you've succeeded!"
)
return 'Mail sent'
The server is "smtp.gmail.com".
The port must match the type of security used.
If using STARTTLS with MAIL_USE_TLS = True, then use MAIL_PORT = 587.
If using SSL/TLS directly with MAIL_USE_SSL = True, then use MAIL_PORT = 465.
Enable either STARTTLS or SSL/TLS, not both.
Depending on your Google account's security settings, you may need to generate and use an app password rather than the account password. This may also require enabling 2-step verification. You should probably set this up anyway.
MAIL_SERVER = 'smtp.gmail.com'
MAIL_PORT = 465
MAIL_USE_SSL = True
MAIL_USERNAME = 'username#gmail.com'
MAIL_PASSWORD = 'app password generated in step 3'
A small but important addition to davidism's answer:
You must have '2-step verification' enabled on your Google account before you're able to set up app-specific passwords.

Django Reset PW emails not sending on AWS

Difficult to phrase this question and what I've done as I don't know where the error is occuring.
I'm using Django hosted on AWS Elastic Beanstalk and SES to send emails. (Only for password reset) However the password reset emails don't seem to be sending.
When I try to send an email as below however it works.
send_mail('Test', 'Email content', 'email#email.com',['email#email.com',])
Also locally the password reset email sends (its put in spam but that's a different issue)
My email settings are:
EMAIL_HOST = 'smtp.gmail.com' # mail service smtp
EMAIL_HOST_USER = 'email#email.com' # email id
EMAIL_HOST_PASSWORD = '****' #password
EMAIL_PORT = 587
# EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django_ses.SESBackend'
AWS_ACCESS_KEY_ID = 'ACCESSKEY'
AWS_SECRET_ACCESS_KEY = 'SECRETKEY'
AWS_SES_REGION_NAME = 'eu-west-1'
AWS_SES_REGION_ENDPOINT = 'email.eu-west-1.amazonaws.com'
DEFAULT_FROM_EMAIL= 'email#email.com'
If it's relevant, anywhere I've written email#email.com is actually the same email.
Any help would be much appreciated.
Thanks

Django and AWS Simple Email Service [duplicate]

This question already has answers here:
Amazon SES SMTP with Django
(8 answers)
Closed 10 months ago.
I'm attempting to get a django site up and running and I'm trying to enable django's standard password reset service.
My site is hosted by AWS EC2, so I figured I would use AWS SES for my email service. However, I can't get the smtp connection to work. Any idea on how to solve for the following error:
Exception Type: SMTPSenderRefused
Exception Value: (530, b'Authentication required', 'example#example.com')
I've looked at the following, but I'd prefer to stay with django's built-in email back-ends if possible:
https://github.com/django-ses/django-ses
https://github.com/azavea/django-amazon-ses
Amazon SES SMTP with Django
Email Settings:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'email-smtp.us-west-2.amazonaws.com'
EMAIL_PORT = 587 # (I've also tried 25, 465, & 2587)
EMAIL_HOST_USER = ''
EMAIL_PASSWORD = ''
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'example#example.com' # but my real email address
My EMAIL_HOST_USER and EMAIL_PASSWORD are set to my SMTP credentials I obtained by following the instructions at: https://docs.aws.amazon.com/ses/latest/DeveloperGuide/smtp-credentials.html
Also, I was sure to verify my DEFAULT_FROM_EMAIL and my domain following these instructions: https://docs.aws.amazon.com/ses/latest/DeveloperGuide/verify-addresses-and-domains.html
And DKIM is enabled and my nameservers have been updated.
Any help would really be appreciated.
UPDATE:
I was able to get the django email send working with the django-ses 3rd party library, but I'm still not able to figure out why the standard backend doesn't work via SMTP.
I think there is definitely something wrong with my code as I went so far as to switch email clients from AWS SES to Zoho mail and I'm still receiving the same 530 Authentication required error.
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.zoho.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'support#example.com'
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'support#example.com'
EMAIL_PASSWORD = ''
If it is asking for Authentication, please double-check the following:
Your SMTP settings
The email that you're trying to send mails from belongs to the verified domain. e.g. if you have verified example.com but you try sending emails from something#gmail.com, it is going to ask for gmail auth credentials.

Configure Flask-Mail to use GMail

When I try to send an email using Flask-Mail to Gmail's SMTP server using the settings below, I get [Errno -2] Name or service not known. How do I fix my configuration to send email with Gmail?
from flask import Flask, render_template, redirect, url_for
from flask_mail import Mail, Message
app = Flask(__name__)
app.config.update(
MAIL_SERVER='smtp#gmail.com',
MAIL_PORT=587,
MAIL_USE_SSL=True,
MAIL_USERNAME = 'ri******a#gmail.com',
MAIL_PASSWORD = 'Ma*****fe'
)
mail = Mail(app)
#app.route('/send-mail/')
def send_mail():
msg = mail.send_message(
'Send Mail tutorial!',
sender='ri******a#gmail.com',
recipients=['ri*********07#msn.com'],
body="Congratulations you've succeeded!"
)
return 'Mail sent'
The server is "smtp.gmail.com".
The port must match the type of security used.
If using STARTTLS with MAIL_USE_TLS = True, then use MAIL_PORT = 587.
If using SSL/TLS directly with MAIL_USE_SSL = True, then use MAIL_PORT = 465.
Enable either STARTTLS or SSL/TLS, not both.
Depending on your Google account's security settings, you may need to generate and use an app password rather than the account password. This may also require enabling 2-step verification. You should probably set this up anyway.
MAIL_SERVER = 'smtp.gmail.com'
MAIL_PORT = 465
MAIL_USE_SSL = True
MAIL_USERNAME = 'username#gmail.com'
MAIL_PASSWORD = 'app password generated in step 3'
A small but important addition to davidism's answer:
You must have '2-step verification' enabled on your Google account before you're able to set up app-specific passwords.

Categories