summaryrefslogtreecommitdiff
path: root/zuul/driver/smtp/smtpconnection.py
blob: c380e5dc4bbe3b75b7b9f405e2b4d76967da458c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Copyright 2014 Rackspace Australia
#
# Licensed 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.

import logging
import smtplib

from email.mime.text import MIMEText

from zuul.connection import BaseConnection
from zuul.lib.logutil import get_annotated_logger


class SMTPConnection(BaseConnection):
    driver_name = 'smtp'
    log = logging.getLogger("zuul.SMTPConnection")

    def __init__(self, driver, connection_name, connection_config):
        super(SMTPConnection, self).__init__(driver, connection_name,
                                             connection_config)

        self.smtp_server = self.connection_config.get(
            'server', 'localhost')
        self.smtp_port = self.connection_config.get('port', 25)
        self.smtp_default_from = self.connection_config.get(
            'default_from', 'zuul')
        self.smtp_default_to = self.connection_config.get(
            'default_to', 'zuul')
        self.smtp_user = self.connection_config.get('user')
        self.smtp_pass = self.connection_config.get('password')
        starttls = self.connection_config.get('use_starttls', 'false')
        if starttls.lower() == 'false':
            self.smtp_starttls = False
        else:
            self.smtp_starttls = True

    def sendMail(self, subject, message, from_email=None, to_email=None,
                 zuul_event_id=None):
        # Create a text/plain email message
        from_email = from_email \
            if from_email is not None else self.smtp_default_from
        to_email = to_email if to_email is not None else self.smtp_default_to

        msg = MIMEText(message)
        msg['Subject'] = subject
        msg['From'] = from_email
        msg['To'] = to_email

        try:
            s = smtplib.SMTP(self.smtp_server, self.smtp_port)
            if self.smtp_starttls:
                s.starttls()
                s.ehlo()
            if self.smtp_user is not None and self.smtp_pass is not None:
                s.login(self.smtp_user, self.smtp_pass)
            s.sendmail(from_email, to_email.split(','), msg.as_string())
            s.quit()
        except Exception as e:
            log = get_annotated_logger(self.log, zuul_event_id)
            log.warning("Error sending mail via SMTP: %s", e)