-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgmail.py
More file actions
111 lines (97 loc) · 3.23 KB
/
Copy pathgmail.py
File metadata and controls
111 lines (97 loc) · 3.23 KB
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
__author__ = 'venkat'
#!/usr/bin/env python
from smtplib import SMTP
from smtplib import SMTPException
from email.mime.text import MIMEText
import sys, argparse
#Global varialbes
subject = "Email from Python script"
EMAIL_RECEIVERS = 'faultmonitoring@gmail.com'
EMAIL_SENDER = 'faultmonitoring@gmail.com'
GMAIL_SMTP = "smtp.gmail.com"
GMAIL_SMTP_PORT = 587
TEXT_SUBTYPE = "plain"
parser = argparse.ArgumentParser();
parser.add_argument("--fromAddr", action='store_true', help='From gmail address');
pswd = 'root@123'
'''subject = 'subject'''
body = 'body'
def showHelp():
print("Usage details")
print("sendGmail -f <sender gmail> -p <send gmail pass> -t <receiver mail> -s <subject> -b <body inside quotes>")
def tokenizeArgs(argList):
arg = 0;
while arg < len(argList):
if argList[arg] == '-f':
global EMAIL_SENDER
EMAIL_SENDER = argList[arg+1]
'''print "Assinging EMAIL_SENDER as ",EMAIL_SENDER'''
arg+=1
elif argList[arg] == '-p':
global pswd
pswd = argList[arg+1]
'''print "Assigning password as ",pswd'''
arg+=1
elif argList[arg] == '-s':
global subject
subject = argList[arg+1]
arg+=1
elif argList[arg] == '-b':
global body
body = argList[arg+1]
arg+=1
elif argList[arg] == '-t':
global EMAIL_RECEIVERS
EMAIL_RECEIVERS = argList[arg+1]
arg+=1
arg = arg + 1
'''print argList '''
def listToStr(lst):
"""This method makes comma separated list item string"""
return ','.join(lst)
def send_email(content):
"""This method sends an email"""
#Create the message
msg = MIMEText(content, TEXT_SUBTYPE)
msg["Subject"] = subject
msg["From"] = EMAIL_SENDER
'''msg["To"] = listToStr(EMAIL_RECEIVERS)'''
msg["To"] = EMAIL_RECEIVERS
print("Preparing mail with...")
print("")
print(msg)
try:
smtpObj = SMTP(GMAIL_SMTP, GMAIL_SMTP_PORT)
#Identify yourself to GMAIL ESMTP server.
smtpObj.ehlo()
#Put SMTP connection in TLS mode and call ehlo again.
smtpObj.starttls()
smtpObj.ehlo()
'''print "EMAIL_SENDER login %s", EMAIL_SENDER'''
'''print "password %s", pswd'''
#Login to service
smtpObj.login(user=EMAIL_SENDER, password=pswd)
#Send email
smtpObj.sendmail(EMAIL_SENDER, EMAIL_RECEIVERS, msg.as_string())
#close connection and session.
smtpObj.quit();
except SMTPException as error:
print("Error: unable to send email : {err}".format(err=error))
def main():
"""This is a simple main() function which demonstrates sending of email using smtplib."""
send_email("Test email was generated by Python using smtplib and email libraries", pswd);
if __name__ == "__main__":
"""If this script is executed as stand alone then call main() function."""
'''if len(sys.argv) == 2:'''
'''
if len(sys.argv) >= 5:
"""main(sys.argv[1])"""
tokenizeArgs(sys.argv)
print "pswd is ", pswd
send_email("Test Message");
else:
"""print "Please provide password"""
showHelp()
'''
send_email("Hello World")
sys.exit(0)