Your Dashboard
Sign-in
Sign-up
RegularPython
  • Membership
  • RoadMaps
  • Online Tests
    • Online Test
    • Interview Questions
    • Online Store
  • Jobs
    • Student Reviews
    • Mock Interviews
    • Apply for jobs
    • Contact for help
  • Sign-in
  • Sign-up

Blog Information

  1. Home
  2. Blog
  3. Blog Information

regularpython@gmail.com

How to send a message through gmail in python?

  • by Sairam Doddaparthi
  • by Dec. 17, 2019, 6:26 a.m.
  • in Python, Regualrpython, Regular Python, Gmail, 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,

  1. How much data inserted into database
  2. How many new records have founded.
  3. Is there any data has missed.
  4. How much unique data is available in database
  5. How many records deleted from database
  6. How many duplicate records inserted into database
  7. How many records failed to insert into database
  8. Any issues or error found while scraping
  9. Do we need to do any data validation
  10. Who is the author of this script.

 

Gmail Code:

 

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(from_='sairamdummy9@gmail.com', to='regularpython@gmail.com', password='sai1@dummy',
              subject='Flipkart data daily status')

gmail.attach_message(html_file_path=os.getcwd()+'\\gmail-status-report.html')
gmail.send_mail()

 

Html Code:

 

<!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>
tags: Python Regualrpython Regular Python Gmail Gmail in python
  • share

User Questions And Answers

Leave Your Request

RegularPython

This website helps to learn ptyhon from basics to advanced level. Everything explained with some practical examples.

  • About
  • Privacy
  • Video Courses
  • Online Test
  • Python Blog

Marthahalli, Bengalore.
regularpython@gmail.com

© Copyright 2020 RegularPython.

  • Sign-in
  • Sign-up