How to send bulk email using python script
Hello, to send bulk emails using email there is so many problem to send each person a seperate mail Instead you can use python to mail in bulk using few lines of code.
Here is the code for sending bulk emails using python.
Process:
- Import libraries
- Set SMTP server and post number which is the mail transfer protocol used to send and receive email across internet. Commonly 587 is the more suitable port for python.
- Now create a secure connection with server using
starttls()
. - Login on the previously created server using email_id and password.
- Now you can create a mail_list of the receivers which you want to send the mail or you can use external files to get mail_list of receivers.
- Create body of the mail you want to send.
- Set body as a message.
- Give appropriate Subject to the message which you want as a mail subject.
- Encode message into string.
- Now you are ready to send mail. Call
send_mail()
with parameterssender_mail_id
by which you login and want to send mail,receiver_mail_id
which can be a single person or a list of people and atlastmessage string
. - Now
quit
the server.
Code:
import smtplib
from email.mime.text import MIMEText
s = smtplib.SMTP("smtp.gmail.com", 587)
print("SMTP Server Created")
s.starttls()
print("TLS started")
s.login("sender_mail_id", "sender_password")
print("Login Successfully")
mail_list = ["mail1@gmail.com", "mail2@gmail.com", "mail3@gmail.com"]
body = """
<b>Congratulations!</b> You've successfully get scripted message.<br>
Go to the Google: <a href="https://www.google.com/">click here</a><br>
Thanks,<br>
<b>Divya Patel<b>
"""
msg = MIMEText(body, 'html')
msg['Subject'] = "DEMO, Scripted message!!"
text = msg.as_string()
s.sendmail("sender_mail_id", mail_list, text)
s.quit()
Advantage of this code:
- E-mail is not going in spam folder.
- You can send your message to bulk of emails in one run.
0 Comments
Do not make spam in comment box.