How to send a message through gmail in python?
How Gmail is useful in pyton and how to create reporting tool with gmail in python?
Please don't forget to subscribe to my channel Thank you very much.
Python has so many useful functionalities. We can create a reporting tool wiht gmail using python.
What is reporting tool here? Reporting tool is nothing but to send the information to client.
Example: You are a python developer and developed webscraping scripts. The work of these scripts are, scrape the data from websites and store it into database. Now client wants some information like this,
- How much data inserted into database
- How many new records have founded.
- Is there any data has missed.
- How much unique data is available in database
- How many records deleted from database
- How many duplicate records inserted into database
- How many records failed to insert into database
- Any issues or error found while scraping
- Do we need to do any data validation
- Who is the author of this script.
import os import smtplib from email.mime.text import MIMEText from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase from email import encoders class Gmail: def __init__(self, subject=None, from_=None, to=None, password=None): self.subject = subject self.from_ = from_ self.to = to self.msg = MIMEMultipart() self.msg['Subject'] = subject self.msg['From'] = from_ self.msg['To'] = to self.password = password def attach_message(self, message=None, html_file_path=None): if message: text = MIMEText(message) self.msg.attach(text) if html_file_path: with open(html_file_path, 'r') as f: html = f.read() text = MIMEText(html, 'html') self.msg.attach(text) def attach_image(self, img_file_path): img_data = open(img_file_path, 'rb').read() image = MIMEImage(img_data, name=os.path.basename(img_file_path)) self.msg.attach(image) def attach_file(self, file_path): # open the file to be sent filename = file_path attachment = open(file_path, "rb") # instance of MIMEBase and named as p p = MIMEBase('application', 'octet-stream') # To change the payload into encoded form p.set_payload((attachment).read()) # encode into base64 encoders.encode_base64(p) p.add_header('Content-Disposition', "attachment; filename= %s" % filename) # attach the instance 'p' to instance 'msg' self.msg.attach(p) def send_mail(self): try: s = smtplib.SMTP('smtp.gmail.com', 587) s.ehlo() s.starttls() s.ehlo() s.login(self.from_, self.password) s.sendmail(self.from_, self.to, self.msg.as_string()) print("Mail send successfully.") s.quit() except Exception as e: print('Mail failed.') print('Found error: ', e) gmail = Gmail(email@example.com', firstname.lastname@example.org', password='sai1@dummy', subject='Flipkart data daily status') gmail.attach_message(html_file_path=os.getcwd()+'\\gmail-status-report.html') gmail.send_mail()
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap Example</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>Flipkart Data Report</h2> <p>Date: 18-12-2019</p> <table class="table table-condensed"> <thead> <tr> <th>Comments</th> <th>Status</th> </tr> </thead> <tbody> <tr> <td>1. How much data inserted into database</td> <td>1,25,000</td> </tr> <tr> <td>2. How many new records have founded.</td> <td>25,000</td> </tr> <tr> <td>3. Is there any data has missed.</td> <td>Yes</td> </tr> <tr> <td>4. How many records failed to insert into database</td> <td>5,000</td> </tr> <tr> <td>5. How much unique data is available in database</td> <td>5,00,000</td> </tr> <tr> <td>6. How many duplicate records inserted into database</td> <td>30,000</td> </tr> <tr> <td>7. How many records deleted from database</td> <td>No</td> </tr> <tr> <td>8. Any issues or error found while scraping</td> <td>No</td> </tr> <tr> <td>9. Do we need to do any data validation</td> <td>Yes</td> </tr> <tr> <td>10. Who is the author of this script</td> <td>Sairam</td> </tr> </tbody> </table> </div> </body> </html>