# Path: modoboa_webmail/lib/attachments.py
# def clean_attachments(attlist):
#     """Remove all attachments from the filesystem
# 
#     :param attlist: a list of 2-uple. Each element must contain the
#                     following information : (random name, real name).
#     """
#     for att in attlist:
#         fullpath = get_storage_path(att["tmpname"])
#         try:
#             os.remove(fullpath)
#         except OSError:
#             pass
# 
# Path: modoboa_webmail/lib/imaputils.py
# def get_imapconnector(request):
#     """Simple shortcut to create a connector
# 
#     :param request: a ``Request`` object
#     """
#     imapc = IMAPconnector(user=request.user.username,
#                           password=request.session["password"])
#     return imapc


import smtplib
from django.core import mail
from django.template.loader import render_to_string
from modoboa.lib.cryptutils import get_password
from modoboa.parameters import tools as param_tools
from . import get_imapconnector, clean_attachments
        )
        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 smtplib.SMTPResponseException as inst:
        return False, {"status": "ko", "error": inst.smtp_error}
    except smtplib.SMTPRecipientsRefused as inst:
        error = ", ".join(["{}: {}".format(rcpt, error)
                           for rcpt, error in inst.recipients.items()])
        return False, {"status": "ko", "error": error}

    # Copy message to sent folder
    sentfolder = request.user.parameters.get_value("sent_folder")

# Path: modoboa_webmail/lib/attachments.py
# def clean_attachments(attlist):
#     """Remove all attachments from the filesystem
# 
#     :param attlist: a list of 2-uple. Each element must contain the
#                     following information : (random name, real name).
#     """
#     for att in attlist:
#         fullpath = get_storage_path(att["tmpname"])
#         try:
#             os.remove(fullpath)
#         except OSError:
#             pass
# 
# Path: modoboa_webmail/lib/imaputils.py
# def get_imapconnector(request):
#     """Simple shortcut to create a connector
# 
#     :param request: a ``Request`` object
#     """
#     imapc = IMAPconnector(user=request.user.username,
#                           password=request.session["password"])
#     return imapc


# Path: modoboa_webmail/lib/sendmail.py
import smtplib
from django.core import mail
from django.template.loader import render_to_string
from modoboa.lib.cryptutils import get_password
from modoboa.parameters import tools as param_tools
from . import get_imapconnector, clean_attachments

        )
        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 smtplib.SMTPResponseException as inst:
        return False, {"status": "ko", "error": inst.smtp_error}
    except smtplib.SMTPRecipientsRefused as inst:
        error = ", ".join(["{}: {}".format(rcpt, error)
                           for rcpt, error in inst.recipients.items()])
        return False, {"status": "ko", "error": error}

    # Copy message to sent folder
    sentfolder = request.user.parameters.get_value("sent_folder")

    get_imapconnector(request).push_mail(sentfolder, msg.message())