I am trying to write a program in Python to automatically populate an email with the name and email of the user using Twitter. This is the definition I am trying to use to find and replace the keywords ('$NAME', '$EMAIL', etc.)
def modifyEmail():
email = open(destinationTitle, 'r+')
for line in email:
email.write(line.replace('$NAME',account.name))
email.write(line.replace('$EMAIL',email))
email.write(line.replace('$USRNAME',targetHandle))
email.write(line.replace('$PROFILEIMG',account.profile_image_url))
email.write(line.replace('$PROFILEURL',account.url))
return
I am using the python-twitter wrapper and the user enters the email address and the Twitter username, but the picture, url and name is taken out of the Twitter account (hence account.name, account.url, etc.)
This is where the destinationTitle
comes from:
destinationTitle = str("emails/"+account.name+" - Generated Spear.emlx")
shutil.copy('email.emlx', destinationTitle)
modifyEmail()
The problem I am having is that it is not replacing the $ words with the correct values, but is doing a strange thing at the bottom whereby it is duplicating the bottom half of the email 5 times. Here is a small example of this, in this example it replaced one $EMAIL at random and I don't know why?! (I'm using a Twitter email for testing):
[...]
From: Twitter <password@twitter.com>
From: Twitter <password@twitter.com>
From: Twitter <password@twitter.com>
To: Adam <$EMAIL>
To: $NAME <me@adam.com>
To: $NAME <$EMAIL>
To: $NAME <$EMAIL>
To: $NAME <$EMAIL>
Subject: Reactivate your Twitter account
Subject: Reactivate your Twitter account
Subject: Reactivate your Twitter account
Subject: Reactivate your Twitter account
Subject: Reactivate your Twitter account
MIME-Version: 1.0
MIME-Version: 1.0
MIME-Version: 1.0
[...]