[파이썬] 대량 메일 발송 자동화

In today’s digital era, email has become an essential communication tool for businesses and individuals alike. However, when it comes to sending a large number of emails, it can be a tedious and time-consuming task. But fear not! Python comes to the rescue with its vast ecosystem of libraries and modules that can automate the process of sending bulk emails. In this blog post, we will explore how to automate bulk email sending using Python.

Prerequisites

Before we dive into the code, make sure you have Python installed on your system. Additionally, we will be using the smtplib module, which is a built-in library in Python for sending emails using the Simple Mail Transfer Protocol (SMTP). So, no additional installations are required.

Setting up SMTP Server

To send emails programmatically, we need to connect to an SMTP server. Most email providers offer SMTP servers that you can use to send emails. You will need the following information to connect to an SMTP server:

You can obtain this information from your email provider’s documentation or settings. Once you have the details, we can move on to the code part.

Example Code

import smtplib

# Define SMTP server settings
smtp_server = 'smtp.example.com'
smtp_port = 587
email_address = 'your_email@example.com'
email_password = 'your_email_password'

# Define email details
subject = 'Important Announcement'
message = 'Dear recipients,\n\nI hope this email finds you well. We have an important announcement to make.\n\nBest Regards,\nYour Name'

# Read recipient list from a file
with open('recipient_list.txt', 'r') as file:
    recipients = file.read().splitlines()

# Connect to SMTP server
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
server.login(email_address, email_password)

# Send email to each recipient
for recipient in recipients:
    to_email = recipient
    email_body = f"Subject: {subject}\n\n{message}"
    server.sendmail(email_address, to_email, email_body)

# Disconnect from the SMTP server
server.quit()

print('Bulk email sending completed successfully!')

Explanation

Let’s go through the code step by step:

  1. Import the smtplib module, which provides a simple way to send emails using SMTP.
  2. Set the SMTP server address, port, and your email address and password.
  3. Define the subject and message of the email.
  4. Read the recipient list from a file. You can create a text file (recipient_list.txt) that contains the email addresses of the recipients, each on a new line.
  5. Connect to the SMTP server using the SMTP function and login to your email account using starttls and login methods.
  6. Iterate over the recipient list and send the email to each recipient using the sendmail method.
  7. Finally, disconnect from the SMTP server using the quit method.

Make sure to replace the placeholders (smtp.example.com, your_email@example.com, your_email_password, recipient_list.txt, etc.) in the code with your actual values.

Conclusion

Automating the process of sending bulk emails can save you a significant amount of time and effort. Python’s smtplib module provides a straightforward way to achieve this. With the example code provided, you can easily customize and integrate it into your own email automation workflows. Happy emailing!