def send_mail(request, form, posturl=None): """Email verification and sending. If the form does not present any error, a new MIME message is constructed. Then, a connection is established with the defined SMTP server and the message is finally sent. :param request: a Request object :param posturl: the url to post the message form to :return: a 2-uple (True|False, HttpResponse) """ if not form.is_valid(): editormode = parameters.get_user(request.user, "EDITOR") listing = _render_to_string( request, "modoboa_webmail/compose.html", { "form": form, "noerrors": True, "body": form.cleaned_data.get("body", "").strip(), "posturl": posturl }) return False, dict(status="ko", listing=listing, editor=editormode) msg = form.to_msg(request) rcpts = prepare_addresses(form.cleaned_data["to"], "envelope") for hdr in ["cc", "cci"]: if form.cleaned_data[hdr]: msg[hdr.capitalize()] = prepare_addresses(form.cleaned_data[hdr]) rcpts += prepare_addresses(form.cleaned_data[hdr], "envelope") try: secmode = parameters.get_admin("SMTP_SECURED_MODE") if secmode == "ssl": s = smtplib.SMTP_SSL(parameters.get_admin("SMTP_SERVER"), int(parameters.get_admin("SMTP_PORT"))) else: s = smtplib.SMTP(parameters.get_admin("SMTP_SERVER"), int(parameters.get_admin("SMTP_PORT"))) if secmode == "starttls": s.starttls() except Exception as text: raise WebmailInternalError(str(text)) if parameters.get_admin("SMTP_AUTHENTICATION") == "yes": try: s.login(request.user.username, get_password(request)) except smtplib.SMTPException as err: raise WebmailInternalError(str(err)) try: s.sendmail(request.user.email, rcpts, msg.as_string()) s.quit() except smtplib.SMTPException as err: raise WebmailInternalError(str(err)) sentfolder = parameters.get_user(request.user, "SENT_FOLDER") get_imapconnector(request).push_mail(sentfolder, msg) clean_attachments(request.session["compose_mail"]["attachments"]) del request.session["compose_mail"] return True, {}
def send_mail(request, form, posturl=None): """Email verification and sending. If the form does not present any error, a new MIME message is constructed. Then, a connection is established with the defined SMTP server and the message is finally sent. :param request: a Request object :param posturl: the url to post the message form to :return: a 2-uple (True|False, HttpResponse) """ if not form.is_valid(): editormode = parameters.get_user(request.user, "EDITOR") listing = _render_to_string( request, "webmail/compose.html", {"form": form, "noerrors": True, "body": form.cleaned_data.get("body", "").strip(), "posturl": posturl} ) return False, dict(status="ko", listing=listing, editor=editormode) msg = form.to_msg(request) rcpts = prepare_addresses(form.cleaned_data["to"], "envelope") for hdr in ["cc", "cci"]: if form.cleaned_data[hdr]: msg[hdr.capitalize()] = prepare_addresses(form.cleaned_data[hdr]) rcpts += prepare_addresses(form.cleaned_data[hdr], "envelope") try: secmode = parameters.get_admin("SMTP_SECURED_MODE") if secmode == "ssl": s = smtplib.SMTP_SSL(parameters.get_admin("SMTP_SERVER"), int(parameters.get_admin("SMTP_PORT"))) else: s = smtplib.SMTP(parameters.get_admin("SMTP_SERVER"), int(parameters.get_admin("SMTP_PORT"))) if secmode == "starttls": s.starttls() except Exception as text: raise WebmailInternalError(str(text)) if parameters.get_admin("SMTP_AUTHENTICATION") == "yes": try: s.login(request.user.username, get_password(request)) except smtplib.SMTPException as err: raise WebmailInternalError(str(err)) try: s.sendmail(request.user.email, rcpts, msg.as_string()) s.quit() except smtplib.SMTPException as err: raise WebmailInternalError(str(err)) sentfolder = parameters.get_user(request.user, "SENT_FOLDER") get_imapconnector(request).push_mail(sentfolder, msg) clean_attachments(request.session["compose_mail"]["attachments"]) del request.session["compose_mail"] return True, {}
def send_mail(request, form, posturl=None): """Email verification and sending. If the form does not present any error, a new MIME message is constructed. Then, a connection is established with the defined SMTP server and the message is finally sent. :param request: a Request object :param posturl: the url to post the message form to :return: a 2-uple (True|False, HttpResponse) """ if not form.is_valid(): editormode = request.user.parameters.get_value("editor") listing = render_to_string( "modoboa_webmail/compose.html", { "form": form, "noerrors": True, "body": form.cleaned_data.get("body", "").strip(), "posturl": posturl }, request) return False, { "status": "ko", "listing": listing, "editor": editormode } msg = form.to_msg(request) conf = dict(param_tools.get_global_parameters("modoboa_webmail")) options = {"host": conf["smtp_server"], "port": conf["smtp_port"]} if conf["smtp_secured_mode"] == "ssl": options.update({"use_ssl": True}) elif conf["smtp_secured_mode"] == "starttls": options.update({"use_tls": True}) if conf["smtp_authentication"]: options.update({ "username": request.user.username, "password": get_password(request) }) try: with mail.get_connection(**options) as connection: msg.connection = connection msg.send() except Exception as inst: raise WebmailInternalError(str(inst)) # Copy message to sent folder sentfolder = request.user.parameters.get_value("sent_folder") get_imapconnector(request).push_mail(sentfolder, msg.message()) clean_attachments(request.session["compose_mail"]["attachments"]) del request.session["compose_mail"] return True, {}
def send_mail(request, posturl=None): """Email verification and sending. If the form does not present any error, a new MIME message is constructed. Then, a connection is established with the defined SMTP server and the message is finally sent. :param request: a Request object :param posturl: the url to post the message form to :return: a 2-uple (True|False, HttpResponse) """ from email.mime.multipart import MIMEMultipart from .forms import ComposeMailForm from modoboa.lib.webutils import _render_to_string from modoboa.lib.cryptutils import get_password form = ComposeMailForm(request.POST) editormode = parameters.get_user(request.user, "EDITOR") if form.is_valid(): from email.mime.text import MIMEText import smtplib body = request.POST["id_body"] charset = "utf-8" if editormode == "html": msg = MIMEMultipart(_subtype="related") submsg = MIMEMultipart(_subtype="alternative") textbody = html2plaintext(body) submsg.attach( MIMEText(textbody.encode(charset), _subtype="plain", _charset=charset)) body, images = find_images_in_body(body) submsg.attach( MIMEText(body.encode(charset), _subtype=editormode, _charset=charset)) msg.attach(submsg) for img in images: msg.attach(img) else: text = MIMEText(body.encode(charset), _subtype=editormode, _charset=charset) if len(request.session["compose_mail"]["attachments"]): msg = MIMEMultipart() msg.attach(text) else: msg = text for attdef in request.session["compose_mail"]["attachments"]: msg.attach(create_mail_attachment(attdef)) set_email_headers(msg, form.cleaned_data["subject"], request.user.encoded_address, form.cleaned_data['to']) if "origmsgid" in form.cleaned_data: msg["References"] = msg["In-Reply-To"] = form.cleaned_data[ "origmsgid"] rcpts = prepare_addresses(form.cleaned_data["to"], "envelope") for hdr in ["cc", "cci"]: if form.cleaned_data[hdr] != "": msg[hdr.capitalize()] = prepare_addresses( form.cleaned_data[hdr]) rcpts += prepare_addresses(form.cleaned_data[hdr], "envelope") try: secmode = parameters.get_admin("SMTP_SECURED_MODE") if secmode == "ssl": s = smtplib.SMTP_SSL(parameters.get_admin("SMTP_SERVER"), int(parameters.get_admin("SMTP_PORT"))) else: s = smtplib.SMTP(parameters.get_admin("SMTP_SERVER"), int(parameters.get_admin("SMTP_PORT"))) if secmode == "starttls": s.starttls() except Exception as text: raise WebmailInternalError(str(text)) if parameters.get_admin("SMTP_AUTHENTICATION") == "yes": try: s.login(request.user.username, get_password(request)) except smtplib.SMTPException, e: raise WebmailInternalError(str(e)) try: s.sendmail(request.user.email, rcpts, msg.as_string()) s.quit() except smtplib.SMTPException, e: raise WebmailInternalError(str(e))
def send_mail(request, posturl=None): """Email verification and sending. If the form does not present any error, a new MIME message is constructed. Then, a connection is established with the defined SMTP server and the message is finally sent. :param request: a Request object :param posturl: the url to post the message form to :return: a 2-uple (True|False, HttpResponse) """ from email.mime.multipart import MIMEMultipart from .forms import ComposeMailForm from modoboa.lib.webutils import _render_to_string from modoboa.lib.cryptutils import get_password form = ComposeMailForm(request.POST) editormode = parameters.get_user(request.user, "EDITOR") if form.is_valid(): from email.mime.text import MIMEText import smtplib body = request.POST["id_body"] charset = "utf-8" if editormode == "html": msg = MIMEMultipart(_subtype="related") submsg = MIMEMultipart(_subtype="alternative") textbody = html2plaintext(body) submsg.attach(MIMEText(textbody.encode(charset), _subtype="plain", _charset=charset)) body, images = find_images_in_body(body) submsg.attach(MIMEText(body.encode(charset), _subtype=editormode, _charset=charset)) msg.attach(submsg) for img in images: msg.attach(img) else: text = MIMEText(body.encode(charset), _subtype=editormode, _charset=charset) if len(request.session["compose_mail"]["attachments"]): msg = MIMEMultipart() msg.attach(text) else: msg = text for attdef in request.session["compose_mail"]["attachments"]: msg.attach(create_mail_attachment(attdef)) set_email_headers( msg, form.cleaned_data["subject"], request.user.encoded_address, form.cleaned_data['to'] ) if "origmsgid" in form.cleaned_data: msg["References"] = msg["In-Reply-To"] = form.cleaned_data["origmsgid"] rcpts = prepare_addresses(form.cleaned_data["to"], "envelope") for hdr in ["cc", "cci"]: if form.cleaned_data[hdr] != "": msg[hdr.capitalize()] = prepare_addresses(form.cleaned_data[hdr]) rcpts += prepare_addresses(form.cleaned_data[hdr], "envelope") try: secmode = parameters.get_admin("SMTP_SECURED_MODE") if secmode == "ssl": s = smtplib.SMTP_SSL(parameters.get_admin("SMTP_SERVER"), int(parameters.get_admin("SMTP_PORT"))) else: s = smtplib.SMTP(parameters.get_admin("SMTP_SERVER"), int(parameters.get_admin("SMTP_PORT"))) if secmode == "starttls": s.starttls() except Exception, text: raise WebmailError(str(text)) if parameters.get_admin("SMTP_AUTHENTICATION") == "yes": try: s.login(request.user.username, get_password(request)) except smtplib.SMTPException, e: raise WebmailError(str(e))