Esempio n. 1
0
    def to_msg(self, request):
        """Convert form's content to an object ready to send.

        We set headers at the end to be sure no one will override
        them.

        """
        msg = self._build_msg(request)
        set_email_headers(
            msg, self.cleaned_data["subject"], request.user.encoded_address,
            self.cleaned_data['to']
        )
        origmsgid = self.cleaned_data.get("origmsgid", None)
        if origmsgid:
            msg["References"] = msg["In-Reply-To"] = origmsgid
        return msg
Esempio n. 2
0
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.auth.lib 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))
Esempio n. 3
0
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.auth.lib 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))