Untitled

 avatar
unknown
python
a year ago
1.1 kB
11
Indexable
from flask import Flask, request, jsonify
from flask_mail import Mail, Message

app = Flask(__name__)

# 設置郵件服務器配置
app.config['MAIL_SERVER'] = 'smtp.gmail.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = 'your_email@gmail.com'
app.config['MAIL_PASSWORD'] = 'your_email_password'
app.config['MAIL_DEFAULT_SENDER'] = 'your_email@gmail.com'

mail = Mail(app)

@app.route('/api/contact', methods=['POST'])
def contact():
    data = request.json
    name = data.get('name')
    email = data.get('email')
    message = data.get('message')

    msg = Message(
        subject=f"聯絡表單來自 {name}",
        recipients=['t7878780@gmail.com'],
        body=f"姓名: {name}\n電子郵件: {email}\n訊息:\n{message}"
    )
    
    try:
        mail.send(msg)
        return jsonify({'message': '郵件已發送!'}), 200
    except Exception as e:
        return jsonify({'message': '郵件發送失敗。', 'error': str(e)}), 500

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)
Editor is loading...
Leave a Comment