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

2 4

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.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

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




A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay