Home > Software design >  How to sent email with gmail with Python Flask but not in spam folder?
How to sent email with gmail with Python Flask but not in spam folder?

Time:07-07

I'm trying to send email with Python Flask but every email from my gmail account is received in the spam folder. This is my code for send the email :

address_list = ['[email protected]']

app = Flask(__name__)
mail= Mail(app)

app.config['MAIL_SERVER']='smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = '[email protected]'
app.config['MAIL_PASSWORD'] = 'password'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
mail = Mail(app)


def send_async_email(msg):
    with app.app_context():
       mail.send(msg)

for x in address_list:
    msg = Message('Hello', sender = '[email protected]', recipients = [x])
    msg.body = "Hello Flask message sent from Flask-Mail"
    send_async_email(msg)
    print("sent")

Is there any way to prevent this from happening? maybe an option to add to make sure that the mails I send are received as normal mails ?

thank you very much

CodePudding user response:

To reduce the chances that messages from your domain are sent to spam or blocked by Gmail, follow the general best practices:

This article is by google team:

  1. Set up valid reverse DNS records of your IP addresses that point to your domain.
  2. Ideally, send all messages from the same IP address. If you must send from multiple IP addresses, use different IP addresses for different types of messages. For example, use one IP address for sending account notifications and a different IP address for sending promotional messages.
  3. Don't mix different types of content in the same messages. For example, don't include content about promotions in purchase receipt messages.
  4. Messages of the same category should have the same email address in the From: header. For example, messages from a domain called solarmora.com might have From: headers like this: Purchase receipt messages: [email protected] Promotional messages: [email protected] Account notification messages: [email protected] Check regularly to make sure your domain isn’t listed as unsafe with Google Safe Browsing. To check your domain status, enter your domain in the Safe Browsing site status page. Also check any domain that’s linked to yours.
  5. Don’t send sample phishing messages or test campaigns from your domain. Your domain’s reputation might be negatively affected, and your domain could be added to internet blocklists. Don’t impersonate other domains or senders without permission. This practice is called spoofing, and it can cause Gmail to mark the messages as spam.
  6. To help prevent valid messages from being marked as spam: Messages that have a From address in the recipient’s Contacts list are less likely to be marked as spam. Occasionally, valid messages might be marked as spam. Recipients can mark valid messages as not spam, so future messages from the sender should be delivered to their inbox.

link to google article

CodePudding user response:

This is how I send mail using flask and it works, so give it a try

from flask_mail import Mail, Message
# This app is: app = Flask(__name__)
from models import app
from config import SendMail

app.config.from_object(SendMail)
mail = Mail(app)


def send_mail():
    msg = Message('Title')
    address = 'receiver_email_id'
    msg.recipients.append(str(address))
    msg.body = "YOUR_MESSAGE"
    mail.send(msg)

Config.py

class SendMail(object):
    MAIL_SERVER = 'smtp.gmail.com'
    MAIL_PORT = 587
    MAIL_USE_TLS = True
    MAIL_USE_SSL = False
    MAIL_USERNAME = 'sender_email_id'
    MAIL_PASSWORD = 'password'
    MAIL_DEFAULT_SENDER = 'sender_email_id'
    MAIL_MAX_EMAILS = None
    MAIL_ASCII_ATTACHMENTS = False
  • Related