Skip to main content

Python script to mail merge with CC

Limitation of normal mail merge is that you can't insert Cc/Bcc fields into it. I have tried to overcome this by writing a small script. The script takes the recipients in To and recipients in Cc fields from an excel sheet. It reads the sheet one by one from both columns and send them emails reading on the contents from an external fields. Yes, it's simple, but what I was trying earlier was something else. I was trying to send 'personalized' emails with 'Cc' field in the mail. But the 'personalized' part is very difficult to implement, as I was not able to write the variable data to the body of the email. So, settled down with this fixed body contents being read from external file. Let me know if anybody is aware of how to send personalized body contents to respective users. I mean the some fields in the body of the mail should on user basis. In this case the body of the mail is general and fixed in nature.

Code to read user lists from excel and send the email inserting Cc field:

import smtplib
import sys
import xlrd
from email.mime.text import MIMEText

def SendEmail(emailTo,CCname):

        # Open the file to read the contents           
        fp = open('C:\.......\mailcontents.txt', 'rb')  
        msg = MIMEText(fp.read())
        fp.close()
       
        #pouplate the msg container fields
        msg['Subject']= "Test mail!"
        msg['From']='myslef@abcorg.com'
        msg['To']=emailTo
        msg['Cc']=CCname
       
              
        # finally send the email
        s = smtplib.SMTP('smtp.abcorg.com')
        s.sendmail(msg['From'], [emailTo,CCname], msg.as_string())
        s.quit() 



# Open the excelsheet containing mail records such as, To, CC
wb=xlrd.open_workbook ('C:\......\mailRecord.xlsx')
wb.sheet_names()
sh=wb.sheet_by_index(0)
for rownum in range(sh.nrows):
        emailTo=str(sh.cell(rownum,0).value)   # The To field in first coloumn
        CCname=str(sh.cell(rownum,1).value)  #The CC field in second coloumn
        SendEmail(emailTo,CCname)  # Call the function above

Comments

Ritesh said…
Hi Nilesh! Hope you're well. For sending personalised emails you can also do what marketing folks have been doing for ages, use the Mail Merge feature in Microsoft Office. Here's some documentation for Office 2003.

http://office.microsoft.com/en-us/word-help/word-mail-merge-a-walk-through-the-process-HA001034920.aspx

Popular posts from this blog

Some one watching where you visited!

Yes... Mozilla has been susceptible to browser-history stealing java script code. Today, Giorgio posted some cool information about the exploit. Mozilla is already working on this. This bug has been reported. Actually they have set up a web site to show the proof-of-concept. Visit www.statrpanic.com in FF,Safari or Netscape and it will tell you which websites have you been already ! But I am not sure it will work in IE or not because my IE is not responding to the website. Clearing history of visited website makes you safe to this attack. I mean this is one way..may be there are other ways to exploit this. But I have found this effective. Try it yourself in FF and then in IE and see the results.

Hijacking SSL

SSL has been in centerstage of researches as well as attacks for quite long time. Last year in a conference in Germany researchers showed how to generate duplicate certificates exploiting MD5 hashing to break SSL. Later in Black Hat, Maxie showed how to exploit a field in SSL certificates to sign an own forged certificate to present it to the client. The main feature of this attack was that the client will never get any warning dialog box by the browser and subsequently the hacker doing an MITM can see the conversation between the client and server. The client will even get a PADLOCK sign to be assured that all things are going via encryption, but in reality it's not. Maxie released a tool SSLStrip to carry out these attacks. The tool has been used by many researchers around the world to carry out the attacks. They all used Unix machines as many open source utilities makes it easier to run the tool on it. My attempt was to run the tool on a Windows machine. It has been never easy t...

Anti CSRF header

Recently I came across an application which was preventing crsf attacks using a unique non-traditional approach. In traditional approach the csrf is thwarted by embedding unique random tokens, called nonce, in each sensitive page. But this application, which was making ajax calls and used jQuery, was creating a header to identify the valid and invalid requests altogether. The idea is to generate a custom header, x-session-token in this case, with every request which is considered sensitive and includes any sort of transaction. For example: xhr.setRequestHeader('x-session-token', csrf_token)   At the server level, server checks for this header if found request is fulfilled, otherwise rejected. We need to use xhr calls for making use of this technique, not useful in regular POST and GET requests. Since, I was not aware of this kind of countermeasures, probably, since most of the applications I did were using standard requests. So, I searched a bit and found even Go...