from flask_mail import Mail, Message app = Flask(__name__) app.config['MAIL_SERVER'] = 'smtp.gmail.com' app.config['MAIL_PORT'] = 465 app.config['MAIL_USE_SSL'] = True app.config['MAIL_USERNAME'] = '[email protected]' app.config['MAIL_PASSWORD'] = 'your-password' mail = Mail(app) @app.route('/') def send_email(): msg = Message('Hello', sender='[email protected]', recipients=['[email protected]']) msg.body = 'This is a test email' mail.send(msg) return 'Email sent!'
from flask_mail import Mail, Message from flask import render_template app = Flask(__name__) app.config['MAIL_SERVER'] = 'smtp.gmail.com' app.config['MAIL_PORT'] = 465 app.config['MAIL_USE_SSL'] = True app.config['MAIL_USERNAME'] = '[email protected]' app.config['MAIL_PASSWORD'] = 'your-password' mail = Mail(app) @app.route('/') def send_email(): msg = Message('Hello', sender='[email protected]', recipients=['[email protected]']) msg.html = render_template('email_template.html') with app.open_resource('path/to/file.pdf') as pdf: msg.attach('file.pdf', 'application/pdf', pdf.read()) mail.send(msg) return 'Email sent!'The package library used in these examples is Flask-Mail. It can be installed via pip using the command: `pip install Flask-Mail`.