Monday, June 25, 2018

python flask mail send example using smtp protocol with google less secure app access

from flask import Flask, Blueprint, url_for
from flask_mail import Mail, Message
import os

mail_settings = {
"MAIL_SERVER": 'smtp.gmail.com',
"MAIL_PORT": 587,
"MAIL_USE_TLS": True,
"MAIL_USE_SSL": False,
"MAIL_USERNAME": 'acc@gmail.com',
"MAIL_PASSWORD": 'password',
"DEBUG": True
}

app = Flask(__name__)
app.config.update(mail_settings)

mail = Mail(app)
site = Blueprint('site', __name__)


@site.route('/')
def index():
return '<a href="' + url_for('site.send') + '">send</a>'


@site.route('/send_mail')
def send():
msg = Message(subject="Hello",
sender=app.config.get("MAIL_USERNAME"),
recipients=["boroog@gmail.com"],
body="This is a test email I sent with Gmail and Python!")
mail.send(msg)
return 'SENT'

# flask_mail сан ашиглахгүй гэвэл доорх код бас ажиллана

# # SMTP_SSL Example
# import smtplib
# # server_ssl = smtplib.SMTP_SSL("smtp.gmail.com", 465)
# smtpserver = smtplib.SMTP("smtp.gmail.com", 587)
# smtpserver.ehlo()
# smtpserver.starttls()
# smtpserver.ehlo()
# smtpserver.login("acc@gmail.com", "password")
# # ssl server doesn't support or need tls, so don't call server_ssl.starttls()
# smtpserver.sendmail("acc@gmail.com", "boroog@gmail.com", "test message")
# #server_ssl.quit()
# smtpserver.close()
return 'SENT'


app.register_blueprint(site)


if __name__ == '__main__':
app.run('127.0.0.1', 5000)

1 comment:

Sergey said...

Ukrainian software developers have a strong skill set, they are tech savvy, up to date with recent innovations and have an independent thinking. Moreover, Ukraine offers intermediate prices of $25-$49/hour for its software developers in comparison to the engineers from other eastern European countries. Learn more about software development companies in Ukraine.