Last Updated on: 10th November 2023, 08:03 pm
This is the Email Marketing tool that will read email lists in a .txt file and send emails to all of them with one click. There is no need to purchase any tool for your small business email marketing.
Sending emails to thousands of recipients can be a complex task. Here is a simple Python program that can send an email to each address in a given list of email addresses present in a TXT file.
Again, as emphasized earlier, it’s crucial you have explicit permission from the recipient to send them an email. Unsolicited emails can be reported as spam, can be illegal, and can cause your email address to be blacklisted.
Email Marketing Tool with Python Complete Code
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# config
mail_content = 'Hello, this is a simple email.'
sender_address = '[email protected]'
sender_pass = 'your-password'
subject = "Hello!"
def send_mail(to_address):
# Setup smtp session
session = smtplib.SMTP('smtp.gmail.com', 587)
session.starttls()
# Login to server
session.login(sender_address, sender_pass)
# Create email
mail = MIMEMultipart()
mail['From'] = sender_address
mail['To'] = to_address
mail['Subject'] = subject
mail.attach(MIMEText(mail_content, 'plain'))
# Send email
text = mail.as_string()
session.sendmail(sender_address, to_address, text)
# End smtp session
session.quit()
# read emails from txt file
with open("emails.txt") as f:
email_list = f.read().splitlines()
# send email to all email addresse in list
for email in email_list:
send_mail(email)
print('All emails sent.')
Replace ‘[email protected]’ and ‘your-password’ with your Gmail email and password. The file ’emails.txt’, contains your email list, with one email per line.
Please ensure that you have ‘Less secure app access’ enabled for your Gmail account, find it in the security settings of your Google Account.
Email Marketing Python Code with Explanation
This Python script is a simple mail system script that uses Gmail’s SMTP server to send emails to multiple recipients. Here’s a line-by-line explanation of the code and how to use it:
Adding essential Python libraries:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
This part of the code imports all the necessary Python modules for the script to work. The smtplib
library is for sending emails using the Simple Mail Transfer Protocol (SMTP). email.mime.multipart
and email.mime.text
are modules to handle email formatting.
Defining email content and sender’s credentials:
# config
mail_content = 'Hello, this is a simple email.'
sender_address = '[email protected]'
sender_pass = 'your-password'
subject = "Hello!"
Here, you define the email’s body, the sender’s email address, the sender’s password, and the subject of the email. Replace the placeholder values with your actual email text, sender email, sender password, and subject.
Creating the send_mail
function:
def send_mail(to_address):
# Setup smtp session
session = smtplib.SMTP('smtp.gmail.com', 587)
session.starttls()
This code creates a new function, send_mail
, to send an email. It starts by setting up the SMTP session using Gmail’s SMTP server and the starttls() method to upgrade the plain SMTP session to a secure one.
Logging into Gmail’s SMTP server:
# Login to server
session.login(sender_address, sender_pass)
This code logs into the Gmail SMTP server using the senders email address and password defined earlier.
Creating the email:
# Create email
mail = MIMEMultipart()
mail['From'] = sender_address
mail['To'] = to_address
mail['Subject'] = subject
mail.attach(MIMEText(mail_content, 'plain'))
The script initiates the email using MIMEMultipart()
. It then sets the ‘From’, ‘To’, and ‘Subject’ headers of the email. The body of the email is attached using MIMEText
, specifying that it is plain text.
Sending the email:
# Send email
text = mail.as_string()
session.sendmail(sender_address, to_address, text)
The as_string()
method converts the MIMEMultipart()
object to a string, which session.sendmail
accepts and sends the email.
Ending SMTP session:
# End smtp session
session.quit()
The quit()
function terminates the SMTP session.
Reading emails.txt
file:
# read emails from txt file
with open("emails.txt") as f:
email_list = f.read().splitlines()
This code opens emails.txt
, reads its content, and splits lines into email addresses, which are stored in email_list
.
Sending email to each address:
# send email to all email addresse in list
for email in email_list:
send_mail(email)
The script uses a for loop to iterate over email_list
. For each email address, it calls the send_mail
function.
Prints confirmation text:
print('All emails sent.')
The script prints ‘All emails sent.’ to the console when it has finished sending all emails.
Remember, it’s critical to have algorithms in place for handling unsubscribes, checking whether emails were sent successfully, and maintaining compliance with laws pertaining to bulk emailing. Services like Mailchimp are specially built for this purpose.
Keep in mind: If you’re sending a large number of emails, you should consider using a dedicated email marketing service that can handle unsubscribes, bounces, IP reputation, etc. for you. It’s also recommended to implement error handling for the case where the email fails to send.