from flask import Flask from flask_mail import Mail, Message app = Flask(__name__) # configure email settings app.config['MAIL_SERVER'] = 'smtp.gmail.com' app.config['MAIL_PORT'] = 465 app.config['MAIL_USE_SSL'] = True app.config['MAIL_USERNAME'] = 'YOUR_EMAIL_HERE' app.config['MAIL_PASSWORD'] = 'YOUR_PASSWORD_HERE' mail = Mail(app)
@app.route('/send_email') def send_email(): msg = Message('Hello', sender='[email protected]', recipients=['[email protected]']) msg.body = "This is a test email sent from Flask-Mail!" mail.send(msg) return "Email sent!"
@app.route('/send_email_html') def send_email_html(): msg = Message('Hello HTML', sender='[email protected]', recipients=['[email protected]']) msg.html = "This is an HTML email sent from Flask-Mail!
" mail.send(msg) return "Email sent!"
@app.route('/send_email_attachments') def send_email_attachments(): with app.open_resource("example.png") as fp: msg = Message('Hello Attachments', sender='[email protected]', recipients=['[email protected]']) msg.attach("example.png", "image/png", fp.read()) mail.send(msg) return "Email sent!"The package library used in these examples is Flask-Mail.