aboutsummaryrefslogtreecommitdiffstats
path: root/opnfv_testapi/common/utils.py
blob: b432212a4c3041a33563bbe812ba500290d7590f (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
##############################################################################
# Copyright (c) 2019 opnfv.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

import logging
import smtplib
from email.mime.text import MIMEText

LOG = logging.getLogger(__name__)
LOG.setLevel(logging.DEBUG)


def send_email(subject, content):
    MAIL_LIST = ['cvp@opnfv.org']
    HOST = "smtp.gmail.com"
    USER = "opnfv.cvp"
    PASSWD = "opnfv@cvp"

    sender = 'cvp<{}@gmail.com>'.format(USER)
    msg = MIMEText(content, _subtype='plain')
    msg['Subject'] = subject
    msg['From'] = sender
    msg['To'] = ";".join(MAIL_LIST)

    _send_email(HOST, sender, USER, PASSWD, MAIL_LIST, msg)


def _send_email(host,
                sender,
                user,
                passwd,
                receivers,
                msg):

    client = smtplib.SMTP()
    try:
        client.connect(host, 25)
        LOG.debug('Success to connect server')
        client.starttls()
        client.login(user, passwd)
        LOG.debug('Success to login')
        LOG.debug('Start to sending email')
        client.sendmail(sender, receivers, msg.as_string())
        client.close()
    except Exception:
        LOG.exception('Error when sending email')
        raise