def save_attachment(f): """Save a new attachment to the filesystem. The attachment is not saved using its own name to the filesystem. To avoid conflicts, a random name is generated and used instead. :param f: an uploaded file object (see Django's documentation) :return: the new random name """ from tempfile import NamedTemporaryFile dstdir = os.path.join(settings.MEDIA_ROOT, "webmail") try: fp = NamedTemporaryFile(dir=dstdir, delete=False) except Exception, e: raise WebmailError(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.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))
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)) try: s.sendmail(request.user.email, rcpts, msg.as_string()) s.quit() except smtplib.SMTPException, e: raise WebmailError(str(e)) sentfolder = parameters.get_user(request.user, "SENT_FOLDER") IMAPconnector(user=request.user.username, password=request.session["password"]).push_mail( sentfolder, msg) clean_attachments(request.session["compose_mail"]["attachments"]) del request.session["compose_mail"] return True, dict(url=get_current_url(request)) listing = _render_to_string( request, "webmail/compose.html", { "form": form, "noerrors": True, "body": request.POST["id_body"].strip(), "posturl": posturl