Add in to send multiple mails at a time
Here is an updated version of the script that allows you to send multiple emails at once:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# Define the SMTP server and port
SMTP_SERVER = "smtp.gmail.com"
SMTP_PORT = 587
# Define the sender's email address and password
SENDER_EMAIL = "[email protected]"
SENDER_PASSWORD = "your_password"
# Define the recipient's email address
RECIPIENT_EMAIL = "[email protected]"
# Define the subject and body of the email
SUBJECT = "Test email from Python"
BODY = "This is a test email sent from Python."
# Define the list of emails to send
EMAILS_TO_SEND = [
{"to": "[email protected]", "subject": "Test email 1", "body": "This is a test email 1."},
{"to": "[email protected]", "subject": "Test email 2", "body": "This is a test email 2."},
{"to": "[email protected]", "subject": "Test email 3", "body": "This is a test email 3."},
# Add more emails to send here...
]
# Create a SMTP connection
server = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
server.starttls()
server.login(SENDER_EMAIL, SENDER_PASSWORD)
# Send each email in the list
for email in EMAILS_TO_SEND:
msg = MIMEMultipart()
msg["From"] = SENDER_EMAIL
msg["To"] = email["to"]
msg["Subject"] = email["subject"]
msg.attach(MIMEText(email["body"], "plain"))
# Send the email
server.sendmail(SENDER_EMAIL, email["to"], msg.as_string())
# Close the SMTP connection
server.quit()
This script uses a list of dictionaries to store the emails to send, where each dictionary contains the recipient's email address, subject, and body of the email. The script then loops through the list and sends each email using the sendmail
method of the SMTP connection.
You can add more emails to send by adding more dictionaries to the EMAILS_TO_SEND
list. For example:
EMAILS_TO_SEND = [
{"to": "[email protected]", "subject": "Test email 1", "body": "This is a test email 1."},
{"to": "[email protected]", "subject": "Test email 2", "body": "This is a test email 2."},
{"to": "[email protected]", "subject": "Test email 3", "body": "This is a test email 3."},
{"to": "[email protected]", "subject": "Test email 4", "body": "This is a test email 4."},
{"to": "[email protected]", "subject": "Test email 5", "body": "This is a test email 5."},
# Add more emails to send here...
]
Make sure to replace the SENDER_EMAIL
and SENDER_PASSWORD
variables with your own email address and password. Also, make sure to replace the RECIPIENT_EMAIL
variable with the email address of the person you want to send the emails to.
Note that this script uses the starttls
method to establish a secure connection to the SMTP server. This is recommended to prevent your email password from being sent in plain text.