Send email to a list of addresses with Python

by Daniel Pham
Published: Last Updated on 0 views

Send email to a list of addresses with Python, how we gonna do that? I will show you a simple way to do that in this post.

First one, I think there are many guys like me, want to send an email to a list of email addresses. When I operating the system for the company or client, many times I faced this request.

So, instead of finding a software or a solution in the internet. Yeah, I wrote one my own.

About the Python script

Here is the link to my Python script: https://github.com/dungpham91/python-send-email-list

python-script-to-send-an-email-to-a-list-of-email-addresses
Python script to send an email to a list of email addresses.

Some features about my script:

  • Supporting to send an email with SMTP account, included all the ports: 25, 465, 587.
  • Can send an email with HTML template, by this feature, you can design the email’s content as you want and make it cool with images, title, links, etc.
  • Supporting to easy configure with the config file.
  • And supporting to add a list of email addresses who will receive the email.
  • Also, the script support the log file to catch all the activities when using the script.

And the most thing that my script is completely free, you can use and share it to anyone.

Send email to a list of addresses with Python script

Now, we going to use the script and below are the steps, just follow it.

Clone the repository to your local machine

Yep, I’m not bundling it as a package or a execute software. So, you have to clone it into your local machine.

This step is requires some skills about Git.

I recommended that you should use the Linux OS or WSL on Windows. By it, you can run the script easier.

And also, you will need to check the Python is pre-installed. If not, please go though the Python installation guide.

Command to clone:

git clone https://github.com/dungpham91/python-send-email-list.git

Change the SMTP configuration

Now, after you cloned the repository into your local machine. Move to the folder python-send-email-list and open the file config.ini.

Please change the information as your.

# Change all the values to your own
[SMTP]
# The SMTP server address you want to use
host = smtp.gmail.com
# Port SMTP non-encrypt is 25, SSL is 465, TLS is 587
port = 465
# Email address used to authenticate with the SMTP server
username = [email protected]
# Email password used to authenticate with SMTP server
password = yourpassword
# The email address used to send the email, it will show up in the From field on the recipient's email.
# You can leave it the same as username
sender = [email protected]
# The name that will be displayed for the sending email address
sender_name = 'No-Reply'

I already commented for each value in the file, so you can adjust it easily.

Update the recipient list

Next, you have to open the file emails.txt and add all your email addresses to this file.

Please notice some points:

  • Each line in the file is an email address.
  • There should be no extra spaces in the email address line.
  • There should be no blank lines in the file.
  • You should process the list before putting in this file.

Edit the email content

Now, I think this is really important because you don’t want to send an email to receivers with my sample content.

Open the file message.html and change anything inside the file as you want. This step might requires some skills about the HTML and CSS. But you can search for some free tools int the internet to generate the HTML template automatically.

the-email-content-as-html-template
The email content as HTML template.

Change the Subject of the email be sent

The second thing is important too here that you will need to change the Subject of the email.

Open the fil send-email.py and at the line 80, type you subject as you want and save it.

...
    # Subject
    subject = "Sample email from Python3"
...

Run the Python script

This is the last step to use the Python script to send an email to the list of email addresses.

Simply, run the below commands:

# Grant the execute permission
chmod +x send-email.py

# Execute the script
./send-email.py

That’s all and depend on how many addresses of the list, the script will take a match time to send the email.

Normally, just few mins to finish.

Check the result

And now, you can check the result of the script’s execution by checking the log file.

Open the file email.log after finished the script.

You gonna see something like this below.

2023-01-29 11:41:31,148 INFO: Start sending email
2023-01-29 11:41:31,149 INFO: Reading SMTP configurations from config.ini
2023-01-29 11:41:31,149 INFO: Finished reading SMTP configurations
2023-01-29 11:41:37,095 INFO: Email sent to abc@devopslite.com
2023-01-29 11:41:41,405 INFO: Email sent to xyz@devopslite.com
2023-01-29 11:41:41,406 INFO: Finished sending email

If something wrong like your SMTP account is not correct, the log file will shows it too.

Then you can check the receiver’s inbox to see the email were sent. Like this one.

the-result-of-the-email-sent-to-the-receivers
The result of the email sent to the receivers.

Conclusion

Okay, that’s all steps about the way to use the Python script to send an email to a bunch of email addresses.

You can use it for your work as anytime. Or just simple that you can use this script to check your SMTP configurations are correct.

Sometime, when I working or managing the Email server, I need to verify the connection with SMTP and I will my script. Hope it will useful to you guys.

0 0 votes
Article Rating

You may also like

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

DevOps Lite is a personal blog specializing in technology with main topics about DevOps, DevSecOps, SRE and System Administrator. Articles are shared for free and contributed to the community.

SUPPORT US

FOLLOW US

Subscribe my Newsletter for new blog posts. Stay updated from your inbox!

© 2021-2024 DevOpsLite.com – All rights reserved.

Please write sources “DevOpsLite.com” when using articles from this website.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

0
Would love your thoughts, please comment.x
()
x

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.