Why am I facing flask-mail issues? - flask

I am not sure what should I do
from flask import Flask
from flask_mail import Mail, Message
app = Flask(__name__)
with app.app_context():
app.config['MAIL_SERVER'] = 'smtp.gmail.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_DEFAULT_SENDER'] = 'sender'
app.config['MAIL_USERNAME'] = 'sender'
app.config['MAIL_PASSWORD'] = '*************'
app.config['MAIL_USE_TSL'] = True
mail = Mail(app)
mail.send_message('Mail sent', sender = 'sender', recipients = ['recipients'], body = 'Mail sent successfully')
It tells me
smtplib.SMTPNotSupportedError: SMTP AUTH extension not supported by server I've even enabled emails from less secure apps in gmail. Why does it not seem to work?
I've tried looking the error up and haven't found much of a satisfactory answer yet.
Edit: I used SSL instead of TLS for the encryption and it seems to show me a different error. Now it tells me smtplib.SMTPSenderRefused: (530, b'5.7.0 Authentication Required. Learn more at\n5.7.0 https://support.google.com/mail/?p=WantAuthError I have no idea what to do next. I don't know how to enable DisplayUnlockCaptcha. https://accounts.google.com/DisplayUnlockCaptcha is the link for it. I proceeded with what the link said, it told "Account access enabled
Please try signing in to your Google Account again from your new device or application." and I retried, still fail to sign in.
Issue has been fixed!

There are several ways you can try, such as activating ssl
app.config['MAIL_PORT'] = 465
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
You must not enable both tls and ssl together
You need to apply a series of settings to your Gmail account, such as disabling 2FA or Display Unlock Captcha, etc.
The steps of the work are outlined in this tutorial.
https://twilio.com/blog/2018/03/send-email-programmatically-with-gmail-python-and-flask.html

Related

SMTPServerDisconnected when sending email from a Django App on Heroku with Mailgun

I'm attempting to send email from a very simple Django app. The app is hosted on Heroku, and I have the Mailgun add-on installed. However, whenever I attempt to actually send an email using Mailgun, I get a SMTPServerDisconnected error, with the message Connection unexpectedly closed.
I've gone through almost every tutorial that I can find on the subject, and have still had no luck. My settings.py is exactly as described in the Heroku Mailgun documentation:
EMAIL_USE_TLS = True
EMAIL_HOST = os.environ.get('MAILGUN_SMTP_SERVER')
EMAIL_PORT = os.environ.get('MAILGUN_SMTP_PORT', '')
EMAIL_HOST_USER = os.environ.get('MAILGUN_SMTP_LOGIN', '')
EMAIL_HOST_PASSWORD = os.environ.get('MAILGUN_SMTP_PASSWORD', '')
The os environment variables referenced are managed by Heroku. When I've searched for similar problems, every other question has been using Port 465, however, my port is on 587, which shouldn't have this issue as far as I know.
Currently, I'm using the sandbox domain provided by Mailgun just to attempt to send a test email to a verified recipient email. The following code is used in my views.py to actually send the email, using the django django.core.mail.send_mail() function.
subject = "Thank you for RSVP-ing!"
message = f'Hello World!'
email_from = "mailgun#blannholley.com"
recipient_list = [request.POST.get('email'), ]
send_mail(subject, message, email_from, recipient_list, fail_silently=False)
Any help would be greatly appreciated, as this is my first time attempting to send email over SMTP, so I'm very lost here and feel like I've tried everything.

AWS SES sending email from Django App fails

I am trying to send mail with using SES and already setup the mail configuration. Now SES running on production mode -not sandbox-. But in Django App, when I try to send mail nothing happens. it's only keep trying to send, no error.
in setting.py made the configuration.
INSTALLED_APPS = [
'django_ses',
]
EMAIL_BACKEND = 'django_ses.SESBackend'
AWS_ACCESS_KEY_ID = config('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = config('AWS_SECRET_ACCESS_KEY')
AWS_SES_REGION_NAME = 'ap-northeast-1'
AWS_SES_REGION_ENDPOINT = 'email-smtp.ap-northeast-1.amazonaws.com'
and email method.
def send_mail(request, user, emails, subject, path):
current_site = get_current_site(request)
message = render_to_string(f"store/emails/{path}.html", {
"some": "context"
})
send_email = EmailMessage(subject, message, "info#mydomain.com", to=emails)
send_email.send()
By the way I already verified the info#mydomain.com in SES console. And when I try to send email from the SES console using send test email option, I can send without a problem. But in Django App. I can't.
Is there any other settings should I do. Because I can't see any error popping when I try to send mail. It's only keep trying to send. But it can't.
AWS SES provides two types of endpoints: API and SMTP
You are using SMTP one
AWS_SES_REGION_ENDPOINT = 'email-smtp.ap-northeast-1.amazonaws.com'
But 'django_ses.SESBackend' working with API type.
Try to set:
EMAIL_BACKEND = 'django_ses.SESBackend'
AWS_SES_REGION_ENDPOINT = 'email.ap-northeast-1.amazonaws.com'
If you want to use usual SMTP connection, you probably need to swap current backend with backends.smtp.EmailBackend and set SMTP connection parameters.

Django is not sending email to admins on error 500 (but email settings do work with send_mail!)

I know, there are plenty of similar questions about this, but none of them helped. When I set the EMAIL_BACKEND to django.core.mail.backends.console.EmailBackend, I do see the email when I trigger a 500. When I set EMAIL_BACKEND to django.core.mail.backends.smtp.EmailBackend, I don't receive the email.. but email manually sent using the send_mail function gets delivered just fine, so the email settings do work. I just don't understand this.
DEBUG = False
EMAIL_BACKEND = env("EMAIL_BACKEND")
EMAIL_HOST = "smtp.sendgrid.net"
EMAIL_HOST_USER = "apikey"
EMAIL_HOST_PASSWORD = env("SENDGRID_API_KEY")
EMAIL_PORT = 587
EMAIL_USE_TLS = True
SERVER_EMAIL = "{my email}"
ADMINS = [
("Kevin Renskers", "{my email}"),
]
MANAGERS = ADMINS
I don't have any custom LOGGING set, and like I said with django.core.mail.backends.console.EmailBackend I do see the email in my console.
So how can it be that I can send email using send_mail, yet errors are not sent to me using the exact same email settings? I've looked in my sendgrid account activity, and yeah they are not sent at all.
Thank you #michjnich for the suggestion to try to call mail-admins manually. When I did I got a clear error:
The from address does not match a verified Sender Identity. Mail cannot be sent until this error is resolved.
Turns out I needed to change SERVER_EMAIL to another email address as configured in Sendgrid. The reason why my manual send_mail call worked fine, is because I gave it the correct from address there.

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

unable to send emails django-helpdesk

I am using django-helpdesk for ticketing system.
its working fine but i am not able to send emails. i am testing this on my localhost.
while i submit a ticket, its getting submitted properly but submitter is not getting any email for newly created ticket.
i added new queue and provided details like E-Mail Address, E-Mail Box Type, E-Mail Hostname, E-Mail Port, Use SSL for E-Mail=False, E-Mail Username, E-Mail Password, IMAP Folder, E-Mail Check Interval properly.
but emails not getting sent.
then i removed all off above from queue and added settings like
QUEUE_EMAIL_BOX_TYPE = IMAP
QUEUE_EMAIL_BOX_SSL = True
QUEUE_EMAIL_BOX_HOST = 'smtp.gmail.com'
QUEUE_EMAIL_BOX_USER = 'email'
QUEUE_EMAIL_BOX_PASSWORD = 'pwd'
but still its not working.
am i missing any settings? please help me out.
QUEUE_EMAIL_BOX settings are not for sending emails, but for receiving them. Configuration for sending emails goes into standard email settings from django.
If you're using SMTP backend (this is default), configuration should be:
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'email'
EMAIL_HOST_PASSWORD = 'pwd'
EMAIL_USE_SSL = True