/
smtp.py
179 lines (146 loc) · 5.41 KB
/
smtp.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
"""
Module for Sending Messages via SMTP
.. versionadded:: 2014.7.0
:depends: - smtplib python module
:configuration: This module can be used by either passing a jid and password
directly to send_message, or by specifying the name of a configuration
profile in the minion config, minion pillar, or master config.
For example:
.. code-block:: yaml
my-smtp-login:
smtp.server: smtp.domain.com
smtp.tls: True
smtp.sender: admin@domain.com
smtp.username: myuser
smtp.password: verybadpass
The resourcename refers to the resource that is using this account. It is
user-definable, and optional. The following configurations are both valid:
.. code-block:: yaml
my-smtp-login:
smtp.server: smtp.domain.com
smtp.tls: True
smtp.sender: admin@domain.com
smtp.username: myuser
smtp.password: verybadpass
another-smtp-login:
smtp.server: smtp.domain.com
smtp.tls: True
smtp.sender: admin@domain.com
smtp.username: myuser
smtp.password: verybadpass
"""
import logging
import os
import socket
import salt.utils.files
log = logging.getLogger(__name__)
HAS_LIBS = False
try:
import email.mime.application
import email.mime.multipart
import email.mime.text
import smtplib
HAS_LIBS = True
except ImportError:
pass
__virtualname__ = "smtp"
def __virtual__():
"""
Only load this module if smtplib is available on this minion.
"""
if HAS_LIBS:
return __virtualname__
return (False, "This module is only loaded if smtplib is available")
def send_msg(
recipient,
message,
subject="Message from Salt",
sender=None,
server=None,
use_ssl="True",
username=None,
password=None,
profile=None,
attachments=None,
):
"""
Send a message to an SMTP recipient. To send a message to multiple \
recipients, the recipients should be in a comma-seperated Python string. \
Designed for use in states.
CLI Examples:
.. code-block:: bash
salt '*' smtp.send_msg 'admin@example.com' 'This is a salt module test' profile='my-smtp-account'
salt '*' smtp.send_msg 'admin@example.com,admin2@example.com' 'This is a salt module test for multiple recipients' profile='my-smtp-account'
salt '*' smtp.send_msg 'admin@example.com' 'This is a salt module test' username='myuser' password='verybadpass' sender='admin@example.com' server='smtp.domain.com'
salt '*' smtp.send_msg 'admin@example.com' 'This is a salt module test' username='myuser' password='verybadpass' sender='admin@example.com' server='smtp.domain.com' attachments="['/var/log/messages']"
"""
if profile:
creds = __salt__["config.option"](profile)
server = creds.get("smtp.server")
use_ssl = creds.get("smtp.tls")
sender = creds.get("smtp.sender")
username = creds.get("smtp.username")
password = creds.get("smtp.password")
if attachments:
msg = email.mime.multipart.MIMEMultipart()
msg.attach(email.mime.text.MIMEText(message))
else:
msg = email.mime.text.MIMEText(message)
msg["Subject"] = subject
msg["From"] = sender
msg["To"] = recipient
recipients = [r.strip() for r in recipient.split(",")]
try:
if use_ssl in ["True", "true"]:
smtpconn = smtplib.SMTP_SSL(server)
else:
smtpconn = smtplib.SMTP(server)
except socket.gaierror as _error:
log.debug("Exception: %s", _error)
return False
if use_ssl not in ("True", "true"):
smtpconn.ehlo()
if smtpconn.has_extn("STARTTLS"):
try:
smtpconn.starttls()
except smtplib.SMTPHeloError:
log.debug("The server didn’t reply properly to the HELO greeting.")
return False
except smtplib.SMTPException:
log.debug("The server does not support the STARTTLS extension.")
return False
except RuntimeError:
log.debug(
"SSL/TLS support is not available to your Python interpreter."
)
return False
smtpconn.ehlo()
if username and password:
try:
smtpconn.login(username, password)
except smtplib.SMTPAuthenticationError as _error:
log.debug("SMTP Authentication Failure")
return False
if attachments:
for f in attachments:
name = os.path.basename(f)
with salt.utils.files.fopen(f, "rb") as fin:
att = email.mime.application.MIMEApplication(fin.read(), Name=name)
att["Content-Disposition"] = 'attachment; filename="{}"'.format(name)
msg.attach(att)
try:
smtpconn.sendmail(sender, recipients, msg.as_string())
except smtplib.SMTPRecipientsRefused:
log.debug("All recipients were refused.")
return False
except smtplib.SMTPHeloError:
log.debug("The server didn’t reply properly to the HELO greeting.")
return False
except smtplib.SMTPSenderRefused:
log.debug("The server didn’t accept the %s.", sender)
return False
except smtplib.SMTPDataError:
log.debug("The server replied with an unexpected error code.")
return False
smtpconn.quit()
return True