You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ponymail.apache.org by hu...@apache.org on 2020/09/09 08:36:08 UTC

[incubator-ponymail-foal] branch master updated: Start work on an email composer endpoint

This is an automated email from the ASF dual-hosted git repository.

humbedooh pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-ponymail-foal.git


The following commit(s) were added to refs/heads/master by this push:
     new 5741c69  Start work on an email composer endpoint
5741c69 is described below

commit 5741c69335566e2713d0c3eea02133c2f3076e25
Author: Daniel Gruno <hu...@apache.org>
AuthorDate: Wed Sep 9 10:35:30 2020 +0200

    Start work on an email composer endpoint
---
 server/endpoints/compose.py | 83 +++++++++++++++++++++++++++++++++++++++++++++
 server/requirements.txt     |  1 +
 2 files changed, 84 insertions(+)

diff --git a/server/endpoints/compose.py b/server/endpoints/compose.py
new file mode 100644
index 0000000..b403366
--- /dev/null
+++ b/server/endpoints/compose.py
@@ -0,0 +1,83 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""Endpoint for sending emails to the list via the UI"""
+import plugins.server
+import plugins.session
+import email.message
+import aiosmtplib
+import fnmatch
+import typing
+import aiohttp.web
+
+
+async def process(
+    server: plugins.server.BaseServer,
+    session: plugins.session.SessionObject,
+    indata: dict,
+) -> typing.Union[dict, aiohttp.web.Response]:
+
+    if not server.config.ui.mailhost:
+        return {"error": "This server has not been set up for sending email yet."}
+
+    # Figure out outgoing MTA
+    mailhost = server.config.ui.mailhost
+    mailport = 25
+    if ':' in mailhost:
+        mailhost, mailport = mailhost.split(':', 1)
+        mailport = int(mailport)
+
+    # Figure out if recipient list is on allowed list
+    to = indata.get('to')
+    mldomain = to.strip("<>").split('@')[-1]
+    allowed_to_send = False
+    for allowed_domain in server.config.ui.sender_domains.split(' '):
+        if fnmatch.fnmatch(mldomain, allowed_domain):
+            allowed_to_send = True
+            break
+    if not allowed_to_send:
+        return {"error": "Recipient mailing list is not an allowed recipient for emails via the web."}
+
+    # If logged in and everything, prep for dispatch
+    if session.credentials and session.credentials.authoritative:
+        subject = indata.get('subject')
+        body = indata.get('body')
+        irt = indata.get('in-repl-to')
+        references = indata.get('references')
+
+        if to and subject and body:
+            msg = email.message.EmailMessage()
+            if irt:
+                msg['in-reply-to'] = irt
+            if references:
+                msg['references'] = references
+            msg['from'] = "%s <%s>" % (session.credentials.name, session.credentials.email)
+            msg['to'] = to
+            msg['subject'] = subject
+            msg['X-Sender'] = "Apache Pony Mail Foal Composer v/0.1"
+            msg.set_charset('utf-8')
+            msg.set_content(body)
+            await aiosmtplib.send(msg, hostname=mailhost, port=mailport)
+            return {"okay": True, "message": "Message dispatched!"}
+        else:
+            return {"error": "You need to fill out both recipient, subject and text body."}
+    else:
+        return {"error": "You need to be logged in via an authoritative source to send emails."}
+
+
+def register(server: plugins.server.BaseServer):
+    return plugins.server.Endpoint(process)
diff --git a/server/requirements.txt b/server/requirements.txt
index 78ab9a6..b842901 100644
--- a/server/requirements.txt
+++ b/server/requirements.txt
@@ -9,3 +9,4 @@ netaddr~=0.8.0
 formatflowed~=2.0.0
 requests~=2.24.0
 google-auth~=1.21.1
+aiosmtplib~=1.1.3
\ No newline at end of file