Sending-Email-Using-Python

Introduction:


  • In this project we are going to send Email using Python code.


Project Directory Structure:



Source Code:

 import smtplib
 from email.message import EmailMessage

 #Login ID and Password of your email
 username = 'email_id'
 password = 'password'

 try:
     data = EmailMessage()

     # senders email address and recipients email address
     data['From'] = 'studyhacks@example.com'
     data['To'] = 'abc@example.com'

     # subject of email
     data['Subject'] = 'This is subject'

     # The content file consist of the body of your message
     with open('content.txt') as file:
         data.set_content(file.read())

     server = smtplib.SMTP('smtp.gmail.com:587')
     server.starttls()
     server.login(username, password)
     server.send_message(data)
     server.quit()

     print("🙂 Email to  '%s'  is sent successfully 🙂 " %data['To'])

 except:
     print("Sorry ☹ Email to  '%s'  is Failed" %data['To'])




Note:

  • Before executing this project, you have to check whether the Less secure app access is Enabled.
  • If it is in Disabled, kindly go and Enable it by tapping the below link.
  • Once that is set (see my screenshot below), it work.



  • The Full Source Code is available on our github site.
                                           Click Here -- github


***********************************************************************************



Post a Comment

0 Comments