mail.py 3.36 KB
Newer Older
1
#
David Trattnig's avatar
David Trattnig committed
2
# Aura Engine (https://gitlab.servus.at/aura/engine)
3
#
David Trattnig's avatar
David Trattnig committed
4
# Copyright (C) 2017-2020 - The Aura Engine Team.
5
#
David Trattnig's avatar
David Trattnig committed
6
7
8
9
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
10
#
David Trattnig's avatar
David Trattnig committed
11
12
13
14
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
15
#
David Trattnig's avatar
David Trattnig committed
16
17
18
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

19

20
21
import smtplib
from email.message import EmailMessage
22
from modules.base.exceptions import MailingException
23
24
25


class AuraMailer():
26
27
28
    """
    Service to send emails to Aura administrators.
    """
29
30
    config = None

31
32


33
    def __init__(self, config):
34
35
36
37
38
39
        """
        Constructor to initialize service with Aura `config`.

        Args:
            config (AuraConfig):    The configuration with the mail server details
        """
40
41
        self.config = config
        self.admin_mails = config.get("admin_mail")
42

43
44
45
46
47
48
49


    #
    #   PUBLIC METHODS
    #


50
    def send_admin_mail(self, subject, body):
51
52
53
54
55
56
57
        """
        Sends an email to the administrator as defined in the configuration.

        Args:
            subject (String):   The email's subject
            body (String):      The email's body text
        """
58
59
60
61
62
        admin_mails = self.admin_mails.split()

        for mail_to in admin_mails:
            self.__send(mail_to, subject, body)

63
64
65
66
67
68
69


    #
    #   PRIVATE METHODS
    #


70
    def __send(self, mail_to, subject, body):
71
72
73
74
75
76
77
        """
        Sends an email to the given address.

        Args:
            subject (String):   The email's subject
            body (String):      The email's body text
        """
78
        # read config
79
80
81
82
83
84
        mail_server = self.config.get("mail_server")
        mail_port = self.config.get("mail_server_port")
        mail_user = self.config.get("mail_user")
        mail_pass = self.config.get("mail_pass")
        from_mail = self.config.get("from_mail")

85
        # check settings
86
87
88
89
90
91
92
93
94
95
96
        if mail_server == "":
            raise MailingException("Mail Server not set")
        if mail_port == "":
            raise MailingException("Mailserver Port not set")
        if mail_user == "":
            raise MailingException("Mail user not set")
        if mail_pass == "":
            raise MailingException("No Password for mailing set")
        if from_mail == "":
            raise MailingException("From Mail not set")

97
        # stuff the message together and ...
98
99
        msg = EmailMessage()
        msg.set_content(body)
100
101
        mailsubject_prefix = self.config.get("mailsubject_prefix")
        if mailsubject_prefix == "":
102
103
            msg["Subject"] = subject
        else:
104
            msg["Subject"] = mailsubject_prefix + " " + subject
105
106
107
        msg["From"] = from_mail
        msg["To"] = mail_to

108
109
110
111
112
113
114
115
116
        # ... send the mail
        try:
            server = smtplib.SMTP(mail_server, int(mail_port))
            server.starttls()
            server.login(mail_user, mail_pass)
            server.send_message(msg)
            server.quit()
        except Exception as e:
            raise MailingException(str(e))