DEV Community

Omale Happiness Ojone
Omale Happiness Ojone

Posted on

How to send emails using python

What are emails?
Emails are messages distributed by electronic means from one computer user to another. There can be many more recipient as well via network.

Methods we can use to send an email:

  1. We can use python web automation using selenium.
  2. We have a python library which is SMTP library which can be used to send an email.
    But for this article I shall be explaining how to use the STMP library.
    STMP which means simple transfer mail protocol. STMP library, this library or the modules defines an SMTP clients session object which can be used to send an email to any other internet machine with an STMP or E-STMP listener daemon.

    Here's the full code

          :
          import smtplib
          server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
          server.login('example@gmail.com', 'password')
          server.sendmail('example@gmail.com', 
          'contact1@gmail.com', 'Hi happiness,how are you?')
          server.quit()
    

Quickly what you should note:
"example@gmail.com"-your email address should be there
"password"-the password to your email address
"contact1@gmail.com"-the receiver's email address.
then you go ahead with the body of the message.

So here's the output
gmail ans.JPG

Finally you have to enable your "less secure app" from your google account in order to send the message.

Top comments (0)