-
Notifications
You must be signed in to change notification settings - Fork 20
/
sendemail.py
64 lines (47 loc) · 1.5 KB
/
sendemail.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
def sendEmail(source, message, subject, toaddress, fromaddress, filename):
# if conflocal.py is not found, import default config.py
# Check for user imports
try:
import conflocal as config
except ImportError:
import config
# Import smtplib for the actual sending function
import smtplib
# Here are the email package modules we'll need
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
COMMASPACE = ', '
# Create the container (outer) email message.
msg = MIMEMultipart()
msg['Subject'] = subject
# me == the sender's email address
# family = the list of all recipients' email addresses
msg['From'] = fromaddress
msg['To'] = toaddress
#msg.attach(message)
mainbody = MIMEText(message, 'plain')
msg.attach(mainbody)
# Assume we know that the image files are all in PNG format
# Open the files in binary mode. Let the MIMEImage class automatically
# guess the specific image type.
if (filename != ""):
fp = open(filename, 'rb')
img = MIMEImage(fp.read())
fp.close()
msg.attach(img)
# Send the email via our own SMTP server.
try:
# open up a line with the server
s = smtplib.SMTP("smtp.gmail.com", 587)
s.ehlo()
s.starttls()
s.ehlo()
# login, send email, logout
s.login(config.mailUser, config.mailPassword)
s.sendmail(config.mailUser, toaddress, msg.as_string())
#s.close()
s.quit()
except:
print("sendmail exception raised")
return 0