-
Notifications
You must be signed in to change notification settings - Fork 2
/
sendmail.py
executable file
·121 lines (98 loc) · 3.72 KB
/
sendmail.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
#!/usr/bin/python3
# Easily send emails with attachments from cron
#
# Mostly stolen from gist https://gist.github.com/srv89/1d3dac6672895f5ca65f
#
# Usage (required params): sendmail.py to=[to_addr] subject="Subject"
# Usage (all possible params: sendmail.py to="to_addr[,to_addr...]" subject="Subject" body="Message body" attach=path/to/folder/|/path/to/file.ext
#
# Attach can be one of the following:
# /path/to/folder/ # will attach everything from this folder (end with /)
# /path/to/file.ext # will attach only thif file
# NOTE: mail will not be sent if attach is defined but file not found
import os, sys
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
import json
conf_file = "/etc/sendmail_py.json"
# Conf file example, remember to chmod correctly!
#
# {
# "comment": "Conf file for misc-scripts-sendmail.py",
# "server": "outlook.office365.com",
# "port" :587,
# "username" : "[email protected]",
# "password" : "MyPassword123"
# }
#
if len(sys.argv) < 2:
sys.exit('USAGE: sendmail.py to=to_addr subject="Subject" [ from=from_addr body="Message body" attach=path/to/folder/|/path/to/file.ext ]')
params = {}
for arg in sys.argv[1:]:
params[arg.split("=")[0]] = arg.split("=")[1]
try:
with open(conf_file) as json_data_file:
conf = json.load(json_data_file)
except:
sys.exit("Unable to read conf file {}".format(conf_file))
smtp_server = conf['server']
smtp_port = conf['port']
username = conf['username'] # Email Address from the email you want to send an email
password = conf['password'] # Password
from_addr = params['from'] if 'from' in params else username
email_list = params['to'].split(',')
subj = params['subject']
html = params['body'] if 'body' in params else subj
attach = params['attach'] if 'attach' in params else None
# Create the body of the message (a HTML version for formatting).
#html = """Add you email body here"""
# Function that send email.
server = smtplib.SMTP('')
def send_mail(username, password, from_addr, to_addrs, msg):
server = smtplib.SMTP(smtp_server, smtp_port)
server.ehlo()
server.starttls()
server.ehlo()
server.login(username, password)
server.sendmail(from_addr, to_addrs, msg.as_string())
server.quit()
# Read email list txt
#email_list = [line.strip() for line in open('email.txt')]
for to_addrs in email_list:
msg = MIMEMultipart()
msg['Subject'] = subj
msg['From'] = from_addr
msg['To'] = to_addrs
# Attach HTML to the email
body = MIMEText(html, 'html')
msg.attach(body)
# Attach Cover Letter to the email
if attach is not None:
file_list = []
if attach.strip()[-1] == "/":
try:
for filename in os.listdir(attach):
file_list.append(attach.strip()+filename)
except:
sys.exit("Attachment directory {} not found.".format(attach))
else:
file_list = [ attach ]
if len(file_list) == 0:
sys.exit("No attachments found in directory")
#sys.exit(file_list.count())
for file in file_list:
try:
open(file)
except:
sys.exit("Attachment file {} not found.".format(file))
cover_letter = MIMEApplication(open(file, "rb").read())
cover_letter.add_header('Content-Disposition', 'attachment', filename=file.split("/")[-1])
msg.attach(cover_letter)
try:
send_mail(username, password, from_addr, to_addrs, msg)
print("Email successfully sent to", to_addrs)
except SMTPAuthenticationError:
print('SMTPAuthenticationError')
print("Email not sent to", to_addrs)