diff options
Diffstat (limited to 'src/jwebmail/webmail.py')
-rw-r--r-- | src/jwebmail/webmail.py | 53 |
1 files changed, 41 insertions, 12 deletions
diff --git a/src/jwebmail/webmail.py b/src/jwebmail/webmail.py index 7e63307..518710c 100644 --- a/src/jwebmail/webmail.py +++ b/src/jwebmail/webmail.py @@ -1,18 +1,21 @@ -from urllib.parse import urlparse +import mimetypes +from email.message import EmailMessage +from io import BytesIO from smtplib import ( SMTP_SSL, - SMTPHeloError, SMTPAuthenticationError, - SMTPNotSupportedError, SMTPException, + SMTPHeloError, + SMTPNotSupportedError, ) -from email.message import EmailMessage +from urllib.parse import urlparse from flask import abort, current_app, flash, redirect, render_template, request, url_for from flask_babel import gettext, lazy_gettext from flask_login import current_user, login_user, logout_user from flask_paginate import Pagination, get_page_parameter, get_per_page_parameter from flask_wtf import FlaskForm +from werkzeug.utils import secure_filename from wtforms import ( EmailField, MultipleFileField, @@ -106,7 +109,7 @@ def displayheaders(folder=""): count = get_read_mails_logged_in().count(folder) page = request.args.get(get_page_parameter(), type=int, default=1) - per_page = request.args.get(get_per_page_parameter(), type=int, default=10) + per_page = request.args.get(get_per_page_parameter(), type=int, default=25) pgn = Pagination( page=page, @@ -208,15 +211,38 @@ def sendmail(): if not form.validate(): abort(400) - # mail = { "attach": form.attachments.data } mail = EmailMessage() - mail["to"] = form.answer_to.data - mail["subject"] = form.subject.data - mail["cc"] = form.cc.data - mail["bcc"] = form.bcc.data - mail["reply"] = form.answer_to.data + mail["To"] = form.send_to.data + mail["Subject"] = form.subject.data + + if form.cc.data: + mail["Cc"] = form.cc.data + if form.bcc.data: + mail["Bcc"] = form.bcc.data + if form.answer_to.data: + mail["Reply"] = form.answer_to.data + mail.set_content(form.content.data) + for f in form.attachments.data: + if not f.filename: + continue + bio = BytesIO() + f.save(bio) + fname = secure_filename(f.filename) + mt, _ce = mimetypes.guess_type(fname) + if mt is None: + abort(400, gettext("invalid mimetype")) + maintype, subtype = mt.split("/") + mail.add_attachment( + bio.getvalue(), maintype, subtype, cte="base64", filename=fname + ) + + if current_app.config["JWEBMAIL"]["WRITE_MAILS"].get("DEBUG"): + print(mail.as_string()) + flash(gettext("succ_send")) + return redirect(url_for("displayheaders"), 303) + try: with SMTP_SSL( current_app.config["JWEBMAIL"]["WRITE_MAILS"]["SMTPS_SERVER"] @@ -229,7 +255,10 @@ def sendmail(): SMTPNotSupportedError, SMTPException, ): - return render_template("writemail.html", form=form, warning=gettext("error_send")), 400 + return ( + render_template("writemail.html", form=form, warning=gettext("error_send")), + 400, + ) flash(gettext("succ_send")) return redirect(url_for("displayheaders"), 303) |