forked from enormandeau/Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gmailsend.py
executable file
·45 lines (36 loc) · 926 Bytes
/
gmailsend.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
#!/usr/bin/env python
"""Send file as message through gmail
Usage:
./gmailsend.py sender recipient subject body_file passwd
"""
# Importing modules
from email.mime.text import MIMEText
import smtplib
import getpass
import sys
# Parse user input
# TODO switch to argparse
try:
sender = sys.argv[1]
recipient = sys.argv[2]
subject = sys.argv[3]
body_file = sys.argv[4]
passwd = sys.argv[5]
except:
print __doc__
sys.exit(1)
# Sends an e-mail to the specified recipient.
SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587
with open(body_file, 'rb') as content:
msg = MIMEText(content.read())
msg['From'] = sender
msg['To'] = recipient
msg['Subject'] = subject
session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
session.ehlo()
session.starttls()
session.ehlo
session.login(sender, passwd) # getpass.getpass("Password: ") )
session.sendmail(sender, recipient, msg.as_string())
session.quit()