update captcha and send email to user

This commit is contained in:
Meewan 2024-08-16 19:15:17 +02:00
parent 4988092706
commit a1f602d313
3 changed files with 23 additions and 25 deletions

View File

@ -5,5 +5,7 @@ db_path = /tmp/contact-form.db
captcha_timout = 3600
[email]
sender = email@example.com
reciever = email@example.com
server = localhost
server = localhost
subject = contact to katzei

View File

@ -5,24 +5,6 @@
<meta charset="utf-8">
<link rel="stylesheet" href="/css/frombulma.css">
<link rel="stylesheet" href="/css/styles.css">
<style type="text/css">
.form-line>.input, .form-line>.textarea{
border-color: #aaaaaa;
width:100%;
margin-bottom: 1em;
}
.form-line>.textarea{
min-height: 25em;
}
.form-error{
width: 100%;
border-radius: 4px;
margin-bottom: 1em;
}
.form-alert{
background-color: rgba(255, 0, 0, 0.2);
}
</style>
</head>
<body>
@ -44,7 +26,7 @@
<label class="label" for="captcha">Vérification dhumanité: combien font {{first_number}} {{op_text}} {{second_number}} ?</label>
</div>
<div class="form-line field">
<input class="input {% if captcha_error %}form-alert{% endif %}" id="captcha" type="number" name="captcha" min="-10" max="100" required />
<input class="input {% if captcha_error %}form-alert{% endif %}" id="captcha" type="number" name="captcha" min="0" max="81" required />
</div>
{% if captcha_error %}
<div class="form-error">Le captcha est invalide veuillez réessayer</div>

View File

@ -52,6 +52,8 @@ def generate_captacha():
first_number = random.randrange(10)
second_number = random.randrange(10)
op_text, op_func = random.choice(OPERATORS)
if op_text == "-" and first_number < second_number:
first_number, second_number = second_number, first_number
result = op_func(first_number, second_number)
captcha = Captcha(
token=token_urlsafe(67),
@ -81,14 +83,24 @@ def validate_captcha(token, value):
def send_email(sender, message):
email = EmailMessage()
email.set_content(message)
email["From"] = sender
email["To"] = config.get("email", "reciever")
katzei_email = EmailMessage()
katzei_email.set_content(message)
katzei_email["From"] = sender
katzei_email["To"] = config.get("email", "reciever")
katzei_email["Subject"] = config.get("email", "subject")
contact_email = EmailMessage()
contact_email.set_content(message)
contact_email["From"] = config.get("email", "sender")
contact_email["To"] = sender
contact_email["Subject"] = config.get("email", "subject")
smtp = SMTP(config.get("email", "server"))
smtp.send_message(email)
smtp.send_message(katzei_email)
smtp.send_message(contact_email)
smtp.quit()
@app.route(config.get("app", "path"), methods=["GET"])
def get_form(context=None):
context = (context or {}) | generate_captacha()
@ -107,3 +119,5 @@ def validate_form():
return get_form(context=context)
send_email(sender=form["email"], message=form["message"])
return flask.render_template("success.html")
application=app