1

I tried to write a script that automatically sends email to a list of email addresses, and want to customize the body content a bit differently using formatter as below:

<html>
Hello {name}, it is me
</html>

So I wrote below script referring to this and had no problem sending the emails to each receivers - but the problem is that email body content accumulates as the number of receiver increases. For example 4th receiver in the list ends up getting an email with 4 body contents, a summation of 1st, 2nd and 3rd receiver's email content. Why is this happening and how can I fix this?

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
import time
import random

fromaddr = "sender_gmail_account"

msg = MIMEMultipart()
msg['From'] = fromaddr
msg['Subject'] = "Subject_line"

#list of receivers
s = open("list.txt", "r")
emails = s.readlines()
print emails
s.close()

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "gmail_password")

for address in emails:
    address = address.strip().split(",")      # Removing White spaces.
    toname = address[0]
    toaddr = address[1].strip()
    if toaddr == "":
        continue

    f = open('mail_template.html')
    body = f.read()
    f.close()

    msg['To'] = toaddr
    body = body.format(name=toname)

    msg.attach(MIMEText(body, 'html'))
    text = msg.as_string()
    body = ""
    server.sendmail(fromaddr, toaddr, text)

    rand = random.randrange(2,5)       # Set range of the waiting time.
    time.sleep(rand)

server.quit()
Layray
  • 105
  • 1
  • 2
  • 9
  • The reason is happening because you should reinstantiate a new "msg" object in your loop. Like this it just changes a few variables of it, but keeps attaching text. – PascalVKooten Nov 30 '15 at 22:27
  • @PascalvKooten Thank you so much. I could have just tried other emailing service but really wanted to know why this happened. Wonder why msg didn't even come to my mind! – Layray Nov 30 '15 at 23:03

1 Answers1

0

yagmail is very nice for writing html emails (essentially, all emails are html emails with simple text as alternative for browsers that cannot deal with html). Note that yagmail will also make it easy to login, among other things.

import yagmail
import random
import time

yag = yagmail.SMTP(fromaddr, 'gmail_password')

template = 'Hello {name}, it is me'           # yagmail automatically makes this HTML
# template = '<h1>Now with big header</h1>'   # other template example     

for address in addresses:
    address = address.strip().split(",")      # Removing White spaces.
    toname = address[0]
    toaddr = address[1].strip()
    if toaddr == "":
        continue

    yag.send(address, "Subject_line", template.format(name=toname))

    time.sleep(random.randrange(2,5))

Get yagmail by using:

pip install yagmail

Going from 48 lines to 19: not bad ;)

Full disclosure: I'm the developer of yagmail.

PascalVKooten
  • 20,643
  • 17
  • 103
  • 160