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()

Monday, 25 May 2020

Deleting the duplicate values from a list and an Array in Python.

While solving business problems, many times we come across a problem wherein we have to remove the duplicate values from the list or an array. The best way to remove the duplicate values from an array or list to convert these to set and then reassign the value of set back to list or array.



Tuesday, 28 April 2020

For Loop in Python

Loops are an essential part of any programing language. This will help not only in reducing the rewriting of code but also helps in iterating over the lists, tuple.

Here in this blog, I will be iterating over the nested list in order to find its two diagonals and then performing the arithmetic functions over it.

Note: I have taken the 2-D array having an equal number of rows and columns.

Sample two dimensional list:-

arr = [[1,2,3], [4,5,6], [7,8,9]]
sum2 = 0sum1 = 0for i,j in zip(reversed(range(len(arr))), range(len(arr))):
    sum1 = sum1 + arr[j][j]
    sum2 = sum2 + arr[j][i]

diff = sum1 - sum2
print(abs(diff))

Output:-

0


Here in the above piece of code, at first I ma adding the two different diagonals of the 2-d array and then I am taking the difference of these two.

Explanation:-

1st diagonal : 1 + 5 + 9 = 15
2nd diagonal : 3 + 5 + 7 = 15

difference will be zero(0).

Tuesday, 21 April 2020

Difference Between Python Lists, Tuples and Sets

Difference Between Python Lists, Tuples and Sets





List Tuple Set
mutable or changeable immutable or unchangeable mutable or changeable
can have duplicate values can have duplicate values don't have duplicate values
eclosed in square brackets [] enclose in round brackets () enclosed in curly brackets
e.g. [1,2,3] e.g. (1,2,3) e.g. {1,2,3}




Kindly provide your feedback.

Wednesday, 11 March 2020

Zip Function In Python

Zip function in python returns a new tuple object where the first element of the tuple will be the combination of the first element of each iterator passed and so on.

e.g. list1 = [1,2,3]
list2 = [4,5,6]

print(tuple(zip(list1,list2)))
output:

((1,4),(2,5),(3,6))

Saturday, 27 April 2019

Comparing Two Columns of Excel and write the result to new excel

It is very common in IT world where we have to compare to compare the two columns of excel and then present a report based on the outcome. In order to perform this activity, we rely on the functionality of Excel to perform this function. However, it is very difficult to use excel when the data is huge or if we have data in any other format(.csv).  In this post, I will be using Python's pandas library to compare the data of two different columns of the same excel. Additionally, we can use this to compare the data of two columns in two different excels as well.

At first, I will be having below data in Book1.xlsx

Here in Book1.xlsx, I have two columns with Note1 and Note2. Both the columns have data in character format. Now, I will be comparing the data in Note1 with Note2, if the data in a single row matches with each other then I will be exporting the whole row into a new excel.

Next, I will be importing the pandas library, reading the data from excel and printing the columns in the excel:-


Now, compare the two columns of the excel and appending the result to a list and then into a dictionary.



After comparing the two columns and taking the matched records into one dictionary, now create a dataframe through this dictionary and then export this dataframe to an excel.



Finally, an excel with the name output.xlsx will be created at the location specified and below is the result in the output.xlsx.




Please do share your thoughts about this blog.

Thank you. Happy learning.

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...