Compare commits
4 Commits
0b4891703d
...
83d6799e53
Author | SHA1 | Date | |
---|---|---|---|
83d6799e53 | |||
4181ff8455 | |||
b58e8c188b | |||
ea8490747f |
39
client/index.js
Normal file
39
client/index.js
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
function message (level, text) {
|
||||||
|
const messageContainer = document.getElementById('contact-mailer-message')
|
||||||
|
const messageElement = document.createElement('p')
|
||||||
|
messageContainer.appendChild(messageElement)
|
||||||
|
messageElement.textContent = text
|
||||||
|
messageElement.classList.add('contact-mailer-message')
|
||||||
|
messageElement.classList.add('contact-mailer-message-'+level)
|
||||||
|
setTimeout(() => {
|
||||||
|
messageContainer.removeChild(messageElement)
|
||||||
|
}, 10000)
|
||||||
|
}
|
||||||
|
|
||||||
|
function interceptForm (formId) {
|
||||||
|
/*
|
||||||
|
* This function intercepts a form submission and send it via XHR.
|
||||||
|
* Param formId is the HTML id of the form
|
||||||
|
*/
|
||||||
|
const formElem = document.getElementById(formId)
|
||||||
|
formElem.onsubmit = async (e) => {
|
||||||
|
e.preventDefault()
|
||||||
|
fetch(formElem.action, {
|
||||||
|
method: formElem.method,
|
||||||
|
body: new FormData(formElem)
|
||||||
|
})
|
||||||
|
.then(data => {
|
||||||
|
if (!data.ok || data.status == 500) {
|
||||||
|
message('error', 'Erreur du service d’envoi. Réessayez plus tard ou contactez https://jean-cloud.net')
|
||||||
|
} else if (data.ok || data.status == 200) {
|
||||||
|
message('success', 'Le message a bien été envoyé !')
|
||||||
|
formElem.reset()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
message('error', 'Impossible d’envoyer le formulaire. Vérifiez votre connexion internet ou réessayez plus tard.')
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
interceptForm ('contact-mailer-form')
|
24
client/style.css
Normal file
24
client/style.css
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
.contact-mailer-form {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
.contact-mailer-message {
|
||||||
|
border: 1px solid;
|
||||||
|
padding: 3px;
|
||||||
|
border-radius: 2px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.contact-mailer-message-error {
|
||||||
|
color: red;
|
||||||
|
border-color: red;
|
||||||
|
}
|
||||||
|
|
||||||
|
.contact-mailer-message-success {
|
||||||
|
color: green;
|
||||||
|
border-color: green;
|
||||||
|
}
|
||||||
|
|
||||||
|
.contact-mailer-message-info {
|
||||||
|
color: blue;
|
||||||
|
border-color: blue;
|
||||||
|
}
|
22
main.py
22
main.py
@ -1,7 +1,6 @@
|
|||||||
import bottle
|
import bottle
|
||||||
request = bottle.request
|
request = bottle.request
|
||||||
response = bottle.response
|
response = bottle.response
|
||||||
redirect = bottle.redirect
|
|
||||||
import smtplib
|
import smtplib
|
||||||
from email.mime.text import MIMEText
|
from email.mime.text import MIMEText
|
||||||
from email.mime.multipart import MIMEMultipart
|
from email.mime.multipart import MIMEMultipart
|
||||||
@ -17,11 +16,6 @@ import datetime # to name unsent mails
|
|||||||
|
|
||||||
##################################################### Bottle stuff ############################################
|
##################################################### Bottle stuff ############################################
|
||||||
|
|
||||||
# The exception that is thrown when an argument is missing
|
|
||||||
class MissingParameterException (Exception):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class StripPathMiddleware(object):
|
class StripPathMiddleware(object):
|
||||||
'''
|
'''
|
||||||
Get that slash out of the request
|
Get that slash out of the request
|
||||||
@ -37,6 +31,11 @@ app = application = bottle.Bottle(catchall=False)
|
|||||||
|
|
||||||
|
|
||||||
##################################################### Configuration ############################################
|
##################################################### Configuration ############################################
|
||||||
|
|
||||||
|
# The exception that is thrown when an argument is missing
|
||||||
|
class MissingParameterException (Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
def get_env(var, default=None):
|
def get_env(var, default=None):
|
||||||
"""var is an env var name, default is the value to return if var does not exist. If no default and no value, an exception is raised."""
|
"""var is an env var name, default is the value to return if var does not exist. If no default and no value, an exception is raised."""
|
||||||
if var in os.environ:
|
if var in os.environ:
|
||||||
@ -92,13 +91,14 @@ mongodb_database = mongodb_client[mongodb_dbname]
|
|||||||
|
|
||||||
@app.post('/submit')
|
@app.post('/submit')
|
||||||
def submission ():
|
def submission ():
|
||||||
# Getting subject
|
# Getting token
|
||||||
if 'token' in request.forms:
|
if 'token' in request.forms:
|
||||||
token = request.forms.getunicode('token')
|
token = request.forms.getunicode('token')
|
||||||
else:
|
else:
|
||||||
response.status = 400
|
response.status = 400
|
||||||
return 'Le jeton d’autentification est requis'
|
return 'Le jeton d’autentification est requis'
|
||||||
|
|
||||||
|
# Getting mail address
|
||||||
if 'mail' in request.forms:
|
if 'mail' in request.forms:
|
||||||
from_address = request.forms.getunicode('mail')
|
from_address = request.forms.getunicode('mail')
|
||||||
else:
|
else:
|
||||||
@ -109,13 +109,11 @@ def submission ():
|
|||||||
try:
|
try:
|
||||||
form = mongodb_database['forms'].find({'token': token})[0]
|
form = mongodb_database['forms'].find({'token': token})[0]
|
||||||
except IndexError as e:
|
except IndexError as e:
|
||||||
save_mail (token, form['mail'], from_address, subject, content)
|
|
||||||
response.status = 400
|
response.status = 400
|
||||||
return 'Le formulaire est introuvable. Le mail a été sauvegardé et sera traité à la main.'
|
return 'Le formulaire demandé est introuvable, merci de vérifier que le token utilisé est le bon'
|
||||||
except pymongo.errors.ServerSelectionTimeoutError as e:
|
except pymongo.errors.ServerSelectionTimeoutError as e:
|
||||||
save_mail (token, form['mail'], from_address, subject, content)
|
|
||||||
response.status = 500
|
response.status = 500
|
||||||
return 'La base de donnée n’est pas accessible. Votre message a été enregistré, il sera remis manuellement à son destinataire.'
|
return 'La base de donnée n’est pas accessible.'
|
||||||
|
|
||||||
try:
|
try:
|
||||||
subject_fields = fill_fields(request, get_fields(form['subject']))
|
subject_fields = fill_fields(request, get_fields(form['subject']))
|
||||||
@ -145,7 +143,7 @@ def submission ():
|
|||||||
|
|
||||||
|
|
||||||
# Redirection
|
# Redirection
|
||||||
#redirect(success_redirect_default)
|
#bottle.redirect(success_redirect_default)
|
||||||
origin = request.headers.get('origin')
|
origin = request.headers.get('origin')
|
||||||
return '<p>Mail envoyé !</p>' + ('<p>Retour au <a href="{}">formulaire de contact</a></p>'.format(origin) if origin else '')
|
return '<p>Mail envoyé !</p>' + ('<p>Retour au <a href="{}">formulaire de contact</a></p>'.format(origin) if origin else '')
|
||||||
|
|
||||||
|
31
test.html
Normal file
31
test.html
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8" />
|
||||||
|
<link rel="stylesheet" href="./client/style.css" />
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="contact-mailer-message"></div>
|
||||||
|
<form action="http://localhost:8080/submit" method="POST" id="contact-mailer-form">
|
||||||
|
<input type="hidden" name="token" value="s0y6WANzU1XnYERoJxMwekP9pqilSVLK5Gbf3hmZadHB2rQ4u8" />
|
||||||
|
<div>
|
||||||
|
<label for="nom">Votre nom :</label>
|
||||||
|
<input type="text" name="nom" required="required"/>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label for="mail">Adresse mail :</label>
|
||||||
|
<input type="email" name="mail" required="required"/>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label for="objet">Objet :</label>
|
||||||
|
<input type="text" name="objet" />
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label for="objet">Votre message :</label>
|
||||||
|
<textarea name="message" required="required"></textarea>
|
||||||
|
</div>
|
||||||
|
<input type="submit" />
|
||||||
|
</form>
|
||||||
|
<script src="./client/index.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
25
test/docker-compose.yml
Normal file
25
test/docker-compose.yml
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
version: '3'
|
||||||
|
services:
|
||||||
|
db:
|
||||||
|
image: mongo
|
||||||
|
|
||||||
|
mailer:
|
||||||
|
build: ..
|
||||||
|
volumes:
|
||||||
|
- ../main.py:/usr/src/app/main.py
|
||||||
|
- ./uwsgi:/tmp/uwsgi
|
||||||
|
depends_on:
|
||||||
|
- db
|
||||||
|
environment:
|
||||||
|
MONGODB_HOST: db
|
||||||
|
SMTP_SERVER_ADDRESS: 'lol'
|
||||||
|
SMTP_SERVER_PORT: 994
|
||||||
|
SMTP_SERVER_USERNAME: toto
|
||||||
|
SMTP_SERVER_PASSWORD: lol
|
||||||
|
SMTP_SERVER_SENDER: moi
|
||||||
|
ADMIN_PASSWORD: admin
|
||||||
|
SMTP_SSL: 'true'
|
||||||
|
proxy:
|
||||||
|
image: nginx
|
||||||
|
ports:
|
||||||
|
- 8080:8080
|
Loading…
Reference in New Issue
Block a user