Reference:
http://rajivpandit.wordpress.com/2013/09/19/how-to-send-email-by-python/
https://docs.python.org/2/library/email-examples.html
[root@server1 ~]# pwd
/root
[root@server1 ~]# cat disk.txt
Filesystem Size Used Avail Use% Mounted on
/dev/simfs 400G 1.4G 399G 1% /
none 4.0G 4.0K 4.0G 1% /dev
none 4.0G 48K 4.0G 1% /dev/shm
[root@server1 ~]# vim mailer.py
#!/usr/bin/python
import os
import smtplib
import email.Utils
# Import the email modules we'll need
from email.mime.text import MIMEText
fp = open('disk.txt', 'rb')
msg = MIMEText(fp.read())
to = "norbuurgen@gmail.com"
FROM = "urgen@localhost.com"
server = smtplib.SMTP('localhost')
server.sendmail(FROM, [to], msg.as_string())
server.quit()
= = = = = = = = = = = === = = === = === = = = =
Following are the two ways to send email notifications by python
“””
This Script Will Send Email Using Internal Email System
In this no need any authentication or password required to send Email
Python Version 2.6
“””
from email.mime.text import MIMEText
import email.Utils
import smtplib
emailList = ["recepient1@gmail.com", "recepient2@gmail.com"] # this is List of Email Ids
for emailID in emailList:
try:
print “Sending email to … “, emailID
FROM = “email@gmail.com”
TO = emailID
message = “This Email is send by Python\n Python is Great !!!”
msg = MIMEText(message)
msg["Subject"] = “Email Notification by Python”
msg["Message-id"] = email.Utils.make_msgid()
msg["From"] = FROM
msg["To"] = TO
host = “smtp.gmail.com”
server = smtplib.SMTP(host)
server.sendmail(FROM, TO, msg.as_string())
server.quit()
print “Email Send”
except Exception, e:
print e
“””
This Script Will Send Email Using Gmail
This Required Username and password
Python Version 2.6
“””
from email.mime.text import MIMEText
from datetime import date
import smtplib
SMTP_SERVER = “smtp.gmail.com”
SMTP_PORT = 587
SMTP_USERNAME = “email@gmail.com”
SMTP_PASSWORD = “password”
EMAIL_TO = ["recepient1@gmail.com", "recepient2@gmail.com"]
EMAIL_FROM = “email@gmail.com”
EMAIL_SUBJECT = “Demo Email : “
DATE_FORMAT = “%d/%m/%Y”
EMAIL_SPACE = “, “
DATA=’This is the content of the email.’
def send_email():
msg = MIMEText(DATA)
msg['Subject'] = EMAIL_SUBJECT + ” %s” % (date.today().strftime(DATE_FORMAT))
msg['To'] = EMAIL_SPACE.join(EMAIL_TO)
msg['From'] = EMAIL_FROM
mail = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
mail.starttls()
mail.login(SMTP_USERNAME, SMTP_PASSWORD)
mail.sendmail(EMAIL_FROM, EMAIL_TO, msg.as_string())
mail.quit()
print “Email Send”
if __name__==’__main__':
send_email()
# # # # # # Send mysql status report # # # # # # #
[root@server1 ~]# vim check.py
import smtplib
from email.mime.text import MIMEText
sql_found = False
io_found = False
with open("status.txt") as logfile:
for line in logfile:
if not io_found and "kSlave_IO_Running: Yes" in line:
print "match1",line
io_found = True
if not sql_found and "Slave_SQL_Running: Yes" in line:
print "match2",line
sql_found = True
if not sql_found or not io_found:
msg=MIMEText("Warning!! Mysql Replicatoin Stopped")
fromaddr="root@DBausit.com"
toaddr="norbuurgen@gmail.com"
server = smtplib.SMTP('localhost')
server.set_debuglevel(1)
server.sendmail(fromaddr, [toaddr], msg.as_string())
server.quit()
else:
print "mysql running OK"
http://rajivpandit.wordpress.com/2013/09/19/how-to-send-email-by-python/
https://docs.python.org/2/library/email-examples.html
[root@server1 ~]# pwd
/root
[root@server1 ~]# cat disk.txt
Filesystem Size Used Avail Use% Mounted on
/dev/simfs 400G 1.4G 399G 1% /
none 4.0G 4.0K 4.0G 1% /dev
none 4.0G 48K 4.0G 1% /dev/shm
[root@server1 ~]# vim mailer.py
#!/usr/bin/python
import os
import smtplib
import email.Utils
# Import the email modules we'll need
from email.mime.text import MIMEText
fp = open('disk.txt', 'rb')
msg = MIMEText(fp.read())
to = "norbuurgen@gmail.com"
FROM = "urgen@localhost.com"
server = smtplib.SMTP('localhost')
server.sendmail(FROM, [to], msg.as_string())
server.quit()
= = = = = = = = = = = === = = === = === = = = =
Following are the two ways to send email notifications by python
“””
This Script Will Send Email Using Internal Email System
In this no need any authentication or password required to send Email
Python Version 2.6
“””
from email.mime.text import MIMEText
import email.Utils
import smtplib
emailList = ["recepient1@gmail.com", "recepient2@gmail.com"] # this is List of Email Ids
for emailID in emailList:
try:
print “Sending email to … “, emailID
FROM = “email@gmail.com”
TO = emailID
message = “This Email is send by Python\n Python is Great !!!”
msg = MIMEText(message)
msg["Subject"] = “Email Notification by Python”
msg["Message-id"] = email.Utils.make_msgid()
msg["From"] = FROM
msg["To"] = TO
host = “smtp.gmail.com”
server = smtplib.SMTP(host)
server.sendmail(FROM, TO, msg.as_string())
server.quit()
print “Email Send”
except Exception, e:
print e
“””
This Script Will Send Email Using Gmail
This Required Username and password
Python Version 2.6
“””
from email.mime.text import MIMEText
from datetime import date
import smtplib
SMTP_SERVER = “smtp.gmail.com”
SMTP_PORT = 587
SMTP_USERNAME = “email@gmail.com”
SMTP_PASSWORD = “password”
EMAIL_TO = ["recepient1@gmail.com", "recepient2@gmail.com"]
EMAIL_FROM = “email@gmail.com”
EMAIL_SUBJECT = “Demo Email : “
DATE_FORMAT = “%d/%m/%Y”
EMAIL_SPACE = “, “
DATA=’This is the content of the email.’
def send_email():
msg = MIMEText(DATA)
msg['Subject'] = EMAIL_SUBJECT + ” %s” % (date.today().strftime(DATE_FORMAT))
msg['To'] = EMAIL_SPACE.join(EMAIL_TO)
msg['From'] = EMAIL_FROM
mail = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
mail.starttls()
mail.login(SMTP_USERNAME, SMTP_PASSWORD)
mail.sendmail(EMAIL_FROM, EMAIL_TO, msg.as_string())
mail.quit()
print “Email Send”
if __name__==’__main__':
send_email()
# # # # # # Send mysql status report # # # # # # #
[root@server1 ~]# vim check.py
import smtplib
from email.mime.text import MIMEText
sql_found = False
io_found = False
with open("status.txt") as logfile:
for line in logfile:
if not io_found and "kSlave_IO_Running: Yes" in line:
print "match1",line
io_found = True
if not sql_found and "Slave_SQL_Running: Yes" in line:
print "match2",line
sql_found = True
if not sql_found or not io_found:
msg=MIMEText("Warning!! Mysql Replicatoin Stopped")
fromaddr="root@DBausit.com"
toaddr="norbuurgen@gmail.com"
server = smtplib.SMTP('localhost')
server.set_debuglevel(1)
server.sendmail(fromaddr, [toaddr], msg.as_string())
server.quit()
else:
print "mysql running OK"
No comments:
Post a Comment