Flask Python: Sending form data with Flask mail

تبليغ
سؤال

يرجى شرح بإيجاز لمإذا تشعر أنك ينبغي الإبلاغ عن هذا السؤال.

تبليغ
‎إلغاء

I there a smooth way on how to send html form data with flask mail?

I am trying to get the input from the user from my contact.html.

{% block content %}
<h1 class=”title”>
Contact Us
</h1>
<form action=”/contact” method=”POST”>
<div class=”field”>
<div class=”control”>
<input class=”input is-large” type=”name” name=”name” placeholder=”Name” autofocus=””>
</div>
</div>

<div class=”field”>
<div class=”control”>
<input class=”input is-large” type=”email” name=”email” placeholder=”Email” autofocus=””>
</div>
</div>

<div class=”field”>
<div class=”control”>
<input class=”textarea is-large” type=”text” name=”message” placeholder=”How can we help you?”>
</div>
</div>

<button type=”submit” class=”button is-block is-info is-large is-fullwidth”>Contact us</button>
</form>
</div>
</div>
{% endblock %}

Here is the main.py @route for this page and the code that CAN send an email but i am not able to format it so it also sends the input from the user.

from flask import Blueprint, render_template, request, flash
from flask_login import FlaskLoginClient, login_required, current_user
from . import db
from flask import Flask
from flask_mail import Mail, Message

app = Flask(__name__)
main = Blueprint(‘main’, __name__)

app.config.update(dict(
MAIL_SERVER = ‘smtp-mail.outlook.com’,
MAIL_PORT = 587,
MAIL_USE_TLS = True,
MAIL_USE_SSL = False,
MAIL_USERNAME = ‘mymail@mail.com’,
MAIL_PASSWORD = ‘mypass’
))

mail = Mail(app)

@main.route(‘/contact’)
def contact():
return render_template(‘contact.html’)

@main.route(‘/contact’, methods=[“POST”])
def contact_post():
if request.method == ‘POST’:
name = request.form.get(‘name’)
email = request.form.get(’email’)
message = request.form.get(‘message’)
msg = Message(‘Test’, sender=’mymail@mail.com’, recipients=[‘mymai@mail.com’])
msg.body = ‘Test mail from:’
mail.send(msg)
return ‘done’

‫أضف إجابة

تصفح
تصفح

مجهول يجيب