在Flask中实现电子邮件发送可以使用Python的smtplib库。下面是一个简单的例子:
from flask import Flask, requestimport smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartapp = Flask(__name__)def send_email(subject, message, to_email): from_email = 'your_email@example.com' password = 'your_password' msg = MIMEMultipart() msg['From'] = from_email msg['To'] = to_email msg['Subject'] = subject msg.attach(MIMEText(message, 'plain')) server = smtplib.SMTP('smtp.example.com', 587) server.starttls() server.login(from_email, password) text = msg.as_string() server.sendmail(from_email, to_email, text) server.quit()@app.route('/send_email', methods=['POST'])def send_email_route(): data = request.get_json() subject = data['subject'] message = data['message'] to_email = data['to_email'] send_email(subject, message, to_email) return 'Email sent successfully!'if __name__ == '__main__': app.run()在上面的例子中,我们定义了一个send_email函数来发送电子邮件。然后我们在Flask应用中创建了一个路由/send_email,通过POST请求发送邮件的主题、内容和收件人邮箱地址。当接收到请求时,我们调用send_email函数发送电子邮件。


