DEV Community

Cover image for Send Web Email With Python Using Smtp and Google Account in bulk
Deepak Raj
Deepak Raj

Posted on • Edited on

Send Web Email With Python Using Smtp and Google Account in bulk

Python is a general multipurpose scripting language. It provides smtplib module, which defines an SMTP client session object that can be used to send mail to any Internet machine with an SMTP or ESMTP listener daemon.

Here is a simple tutorial for sending an email using python. You can also integrate it with Flask/Django or other GUI framework for better presentation

import smtplib
import email.message
server = smtplib.SMTP('smtp.gmail.com:587')

email_content = "Message body"
listofemail =  []    
#Enter Email list here

msg = email.message.Message()
msg['Subject'] = 'Subject of Email'
msg['From'] = 'yourEmail@gmail.com'
password = "your gmail app password"   
# create app password in accounts/security
msg.add_header('Content-Type', 'text/html')
msg.set_payload(email_content)
s = smtplib.SMTP('smtp.gmail.com: 587')
s.starttls()


# Login Credentials for sending the mail
s.login(msg['From'], password)

for dest in listofemail:
    s.sendmail(msg['From'], dest, msg.as_string())
    print(f"sending to {dest}")
Enter fullscreen mode Exit fullscreen mode

Fork me here

GitHub logo Py-Contributors / PyEmailer

Send Emails In One Click With Python.

header

Python 3.7 issues forks stars License

Visitor Count

Blog On Python, Machine Learning and Data Science Visit CodePerfectPLus

Create App Password in gmail.

  • GO to Account setting/Security
  • click app password
  • Select APP -> others, Select Device -> Others
  • Copy paste the code in script.py password variable

Usage

git clone https://github.com/codePerfectPlus/PyEmailer
cd PyEmailer
Enter fullscreen mode Exit fullscreen mode
from src.send_email import PyEmailer

your_email_id = "your_email_id"
your_app_password = "your_app_password"
email_subject = "email_subject_here"
email_content = "<h1> Email Content can be html too</h1>"
listOfEmail = ["destination1@gmail.com", "destination2@gmail.com"]

pyemail = PyEmailer(your_email_id, your_app_password)

if __name__ == "__main__":
    pyemail.sendEmail(email_subject, email_content, listOfEmail)
Enter fullscreen mode Exit fullscreen mode

Upcoming features

  • file attachment in email
  • RegEx to verify the Emails

Project






React ❤️ to encourage Author.

Top comments (1)

Collapse
 
codeperfectplus profile image
Deepak Raj

Source code

GitHub logo codePerfectPlus / Email-Sending-Script-Python

Send Emails In One Click With Python.

Python 3.7 issues forks stars License

Visitor Count

header

Send Emails In One Click With Python.

  • Step 1 Create App Password in gmail.

  • GO to Account setting/Security

  • click app password

  • Select APP -> others, Select Device -> Others

  • Copy paste the code in script.py password variable

  • Step 2 Enter Email address in emaillist.py file.

  • Step 3 Run the Script

Project

footer