Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • Sending Email in bulk using Python

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 3.19k
    Comment on it

    For sending Email to a list of receivers using python can done by using the given code. Here I am using Gmail SMTP service for this purpose (Create a separate account for this, cause it might be dangerous to use your primary email address as the Gmail might suspend you account in case of spamming).

    There are 3 things required

    1. Message written in text file. (sampleMessage.txt)
    2. List of receivers in csv file. (listReceivers.csv)
    3. Edit your Gmail account security to allow access for less secure apps by using the below link. and select turn on.

    Keep your sampleMessage.txt, listReceivers.csv and file under the same directory.

    The content of sampleMessage.txt is

    Hi there, How are you ?

    The content of listReceivers.csv is

    Name, Email

    Here is the code for sending the email to the receivers (

    import csv
    import smtplib
    from email.mime.text import MIMEText
    import re
    fp = open('sampleMessage.txt','rb')
    msg = MIMEText(
    msg['Subject'] = 'Test Email'
    msg['From'] = '' #put the email address of sender
    server = smtplib.SMTP('')
    server.login(msg['From'],'xxxxxxxxxxx')  #put sender's password here 
    email_data = csv.reader(open('listReceiver.csv','rb'))
    email_pattern = re.compile("^.+@.+\..+$")
    flag = 0
    for row in email_data:
                    del msg['To']
                    msg['To'] = row[1]
                            print "Message sent to ", row[0]
                    except SMTPException:
                            flag = 1
                            print "Error occoured while sending the emails"
    if flag == 0:
            print "Messages are sent sucessfully"

    Output after running the program

    Message sent to  Abhishek
    Message sent to  Mukesh
    Message sent to  Ayush
    Message sent to Pankaj
    Messages are sent sucessfully

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: