Wednesday, 14 July 2021

Sending email using Python

Python has provided smtplib library in order to send email.

import smtplib

#domain name for the smtp server and port number
conn = smtplib.SMTP('smtp.gmail.com',587)
type(conn)
conn.ehlo() #ehlo method to connect to the smtp mail server

conn.starttls() #starttle method to start encryption
conn.login(SENDER_EMAIL,SENDER_PASSWORD)
#you need to enter the app specific password in order to login
conn.sendmail(FROM_EMAIL_ADDRESS,TO_EMAIL_ADDRESS,'Subject: Test Email \n
\nDear All, \n So long, and thanks.\n\n-Al')
conn.quit()

Do comment if you like the post.

Sending HTML Email Using Python

While sending email using python, we should use two modules "smtplib" and "email". These two modules will help in creating the connection and building the html body of the email.

The below code will help in creating connection and sending email.

import smtplib

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText


SENDER_EMAIL = 'SENDER_EMAIL'

SENDER_PASSWORD = 'SENDER_PASSWORD' #app password for gmail.

SERVER = 'smtp.gmail.com:587'

RECEIVER_EMAIL = 'SENDER_EMAIL'


SUBJECT = 'Test'

HTML = """

<!DOCTYPE html>

<html>

  <head>

    <meta charset="utf-8" />

    <style type="text/css">

      table {

        background: white;

        border-radius:3px;

        border-collapse: collapse;

        height: auto;

        max-width: 900px;

        padding:5px;

        width: 100%;

        animation: float 5s infinite;

      }

      th {

        color:#D5DDE5;;

        background:#1b1e24;

        border-bottom: 4px solid #9ea7af;

        font-size:14px;

        font-weight: 300;

        padding:10px;

        text-align:center;

        vertical-align:middle;

      }

      tr {

        border-top: 1px solid #C1C3D1;

        border-bottom: 1px solid #C1C3D1;

        border-left: 1px solid #C1C3D1;

        color:#666B85;

        font-size:16px;

        font-weight:normal;

      }

      tr:hover td {

        background:#4E5066;

        color:#FFFFFF;

        border-top: 1px solid #22262e;

      }

      td {

        background:#FFFFFF;

        padding:10px;

        text-align:left;

        vertical-align:middle;

        font-weight:300;

        font-size:13px;

        border-right: 1px solid #C1C3D1;

      }

    </style>

  </head>

  <body>

    Dear Somebody,<br> <br>

    Bla-bla-bla<br><br>

    <table>

      <thead>

        <tr style="border: 1px solid #1b1e24;">

          <th>head1</th>

          <th>head2</th>

          <th>head3</th>

          <th>head4</th>

          <th>head5</th>

          <th>head6</th>

        </tr>

      </thead>

      <tbody>

        <tr>

          <td>row1col1</td>

          <td>row1col2</td>

          <td>row1col3</td>

          <td>row1col4</td>

          <td>row1col5</td>

          <td>row1col6</td>

        </tr>

        <tr>

          <td>row2col1</td>

          <td>row2col2</td>

          <td>row2col3</td>

          <td>row2col4</td>

          <td>row2col5</td>

          <td>row2col6</td>

        </tr>

      </tbody>

    </table>

    <br>

    SENDER_NAME.<br>

    For more assistance please contact our support team:

    <a href='mailto:SENDER_EMAIL'>SENDER_EMAIL</a>.<br> <br>

    Thank you!

  </body>

</html>

"""



def generate_msg() -> MIMEMultipart:

    message = MIMEMultipart("alternative", None, [MIMEText(HTML, 'html')])

    message['Subject'] = SUBJECT

    message['From'] = SENDER_EMAIL

    message['To'] = RECEIVER_EMAIL

    return message



def send_message():

    message = generate_msg()

    server = smtplib.SMTP(SERVER)

    server.ehlo()

    server.starttls()

    server.login(SENDER_EMAIL, SENDER_PASSWORD)

    server.sendmail(SENDER_EMAIL, RECEIVER_EMAIL, message.as_string())

    server.quit()



if __name__ == '__main__':

    send_message()

Sending email using Python

Python has provided smtplib library in order to send email. import smtplib #domain name for the smtp server and port number conn = smtplib.S...