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
    • 2.59k
    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.
    https://www.google.com/settings/security/lesssecureapps and select turn on.

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

    The content of sampleMessage.txt is

    Hi there, How are you ?
    
    Regards,
    Abhishek
    

    The content of listReceivers.csv is

    Name, Email
    Abhishek, abhishek@xyz.com
    Mukesh, mukesh@abc.com
    Ayush, ayushBlueCoder@abc.com
    .
    .
    .
    .
    .
    .
    Pankaj, pankaj@gmail.com
    

    Here is the code for sending the email to the receivers (sendEmail.py)

    import csv
    import smtplib
    from email.mime.text import MIMEText
    import re
    
    fp = open('sampleMessage.txt','rb')
    msg = MIMEText(fp.read())
    fp.close()
    
    msg['Subject'] = 'Test Email'
    msg['From'] = 'sender@gmail.com' #put the email address of sender
    
    server = smtplib.SMTP('smtp.gmail.com:587')
    server.starttls()
    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:
            if( email_pattern.search(row[1])):
                    del msg['To']
                    msg['To'] = row[1]
                    try:
                            server.sendmail(msg['From'],[row[1]],msg.as_string())
                            print "Message sent to ", row[0]
                    except SMTPException:
                            flag = 1
                            print "Error occoured while sending the emails"
    server.quit()
    
    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
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
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: