| Server IP : 85.214.239.14 / Your IP : 216.73.216.27 Web Server : Apache/2.4.65 (Debian) System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Mon Sep 30 15:36:27 MSK 2024 x86_64 User : www-data ( 33) PHP Version : 8.2.29 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /proc/self/root/srv/modoboa/env/lib/python3.5/site-packages/modoboa_webmail/lib/ |
Upload File : |
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 ..exceptions import WebmailInternalError
from . import get_imapconnector, clean_attachments
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, {}