Siz Tutorial | How to use Python to send emails automatically | 20% beneficiary to siz-official | by @faizan2sheikh

in Steem Infinity Zone3 years ago

Send Emails using Python

Hello everyone, I hope y'all are going great. If you use email services like Gmail and receive emails from different companies about their product, have you ever wondered how these many emails are sent to you? Is there any employee the company hired to just send you emails? If you think so then let me tell you that it's not the case, sending promotion material or updates about accounts to the users is a very tedious task, if someone tries to do it manually, then it is highly inefficient and time taking. Apart from a slow work rate, these emails are also prone to mistakes made by the human sender.

p1.png

Method

I will elaborate on the code for you and tell you about everything I used to program my computer to send e-mails to users.

  • Step 1

First of all, you need to create a google account, this account will be used to send emails, I recommend not using your personal account for this purpose.

  • Step 2

After you have created an account you have to make the following changes to allow the account to be linked with our program.
a) Open your account on google accounts and navigate to security

p2.png

b)Scroll the tab and navigate to less secure apps and click it

p3.png

c)Now toggle the button and switch it on

p4.png

  • Step 3

Now we are done with setting up our sender account, let's start coding

import smtplib

smtplib - SMTP protocol client is a module that provides us the ready-made functions that we can use to send emails. So, we need to import this library into our code to use it. We use the keyword "import" followed by the module name to import any module in python code.

  • Step 4

Now we have to connect to the module's server to establish a connection. Next, we have to provide our credentials in code to do so. We have to put the message or body of our mail in the body variable and pass them as arguments to our function.

def send_mail(email,name,bill,body):
    '''This function sends emails to customers for successful transactions'''
    try:
        body=f'Thank You {name} for shopping from Ait It, your total {bill}. We hope that you like our products and shop again.\nRegards: AirIt'
        message = 'Subject: {}\n\n{}'.format('ThankYou For Shopping', body)
        server=smtplib.SMTP('smtp.gmail.com',587)
        server.ehlo()
        server.starttls()
        server.login(YourEmail,YourPassword)
        server.sendmail(YourEmail,email,message)
        server.close()
        print('ThankYou','Confirmation email has been sent, if you entered valid email.')
    except:
        print('Sorry message can not be sent!')

We are using try-except to avoid any potential errors that can be encountered while executing the code.
We passed the following arguments to our function:

  • Email - the email of the user we want to send the email to
  • Name - the name of the user we want to send the email to
  • Bill - I assumed the scenario of the E-commerce app so this represents the user's bill
  • Body - the actual content of our mail

Few important points:

  • YourEmail
    You need to replace the email of the account we created instead of "YourEmail".
  • YourPassword
    You need to replace the password of the account we created instead of "YourPassword".
  • Don't use your personal account for this purpose, as it is not very secure.

Final result

This is an email I sent via this code.

s7.jpg

I hope you guys found this tutorial very helpful, let me know if you want me to post more.Thanks for reading this post. Have a nice day!

Sort:  

Nice post. if you haven't Get the recent prize from bittorent for steem community don't get behind Sign in Now CLICK HERE and claim your BTT now

 3 years ago 

Good one Post dear friend you make a very good post thanks for sharing a good information with us my best wishes for you. Thanks for making a good contents posts for members.

Regards, Faran Nabeel

Keep supporting so I can post more.

Coin Marketplace

STEEM 0.17
TRX 0.15
JST 0.028
BTC 59893.00
ETH 2414.92
USDT 1.00
SBD 2.43