-
Notifications
You must be signed in to change notification settings - Fork 1
/
gmailer.py
executable file
·57 lines (45 loc) · 1.64 KB
/
gmailer.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
import argparse
import smtplib
from datetime import datetime
from email.mime.text import MIMEText
GOOGLE_SMTP_SERVER = 'smtp.gmail.com'
TLS_PORT = 587
def smtp_login(username, password, server=GOOGLE_SMTP_SERVER, port=TLS_PORT):
server = smtplib.SMTP(server, port)
server.starttls()
server.login(username, password)
return server
def create_email(sender, recipient, subject, body):
message = MIMEText(body, 'html')
message['Subject'] = subject
message['From'] = sender
message['To'] = recipient
return message
# configure and parse command-line parameters
parser = argparse.ArgumentParser(
description='Send emails using a Google account.'
)
parser.add_argument('user', help='Google account used to send emails')
parser.add_argument('password', help='Password to authenticate the Google account')
parser.add_argument('to', metavar='recipient', help='Recipient email address')
parser.add_argument('file', type=argparse.FileType('r'), help='Location of the file containing the markup to send')
parser.add_argument('--subject', help='Email subject')
args = parser.parse_args()
# set the email subject if it is not provided
if not args.subject:
args.subject = 'Gmailer test - ' + str(datetime.now())
# login to the Google SMTP server
server = smtp_login(args.user, args.password)
# create the email message
email_body = args.file.read()
email = create_email(
sender=args.user,
recipient=args.to,
subject=args.subject,
body=email_body
)
try:
server.sendmail(args.user, args.to, email.as_string())
print "Email sent successfully"
except smtplib.SMTPException as e:
print "Unable to send email: " + e