-
Notifications
You must be signed in to change notification settings - Fork 0
/
gmail.py
43 lines (38 loc) · 1.31 KB
/
gmail.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
#!/usr/bin/env python
from smtplib import SMTP
from smtplib import SMTPException
from email.mime.text import MIMEText
import sys
import gmailpassword
#Global varialbes
EMAIL_SUBJECT = "MythNetTV has something to tell you"
EMAIL_RECEIVERS = ['[email protected]']
EMAIL_SENDER = '[email protected]'
GMAIL_SMTP = "smtp.gmail.com"
GMAIL_SMTP_PORT = 587
TEXT_SUBTYPE = "plain"
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"] = EMAIL_SUBJECT
msg["From"] = EMAIL_SENDER
msg["To"] = listToStr(EMAIL_RECEIVERS)
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()
#Login to service
smtpObj.login(user=EMAIL_SENDER, password=gmailpassword.gmailpassword)
#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)