Completed
Push — master ( cf8571...7450e5 )
by Tomaz
03:00
created

ResultSets.formatter()   A

Complexity

Conditions 3

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 3
dl 0
loc 8
rs 9.4286
1
import httplib
2
3
import requests
4
5
from st2actions.runners.pythonrunner import Action
6
7
__all__ = [
8
    'SendEmailAction'
9
]
10
11
SEND_EMAIL_API_URL = 'https://api.mailgun.net/v2/%(domain)s/messages'
12
13
14
class SendEmailAction(Action):
15
    def run(self, sender, recipient, subject, text, html=None):
16
        domain = self.config['domain']
17
        api_key = self.config['api_key']
18
19
        data = {
20
            'from': sender,
21
            'to': recipient,
22
            'subject': subject,
23
            'text': text
24
        }
25
26
        if html:
27
            data['html'] = html
28
29
        api_url = SEND_EMAIL_API_URL % {'domain': domain}
30
        response = requests.post(api_url, auth=('api', api_key), data=data)
31
32
        if response.status_code != httplib.OK:
33
            msg = ('Failed to send message (status_code=%s): %s' %
34
                   (response.status_code, response.text))
35
            raise Exception(msg)
36
37
        return True
38