Adding sender name to email

  • Thread starter gillespie.amanda
  • Start date
G

gillespie.amanda

How do I add a Sender name to the emails sent by the following script:

def createhtmlmail (html, text, subject):
"""Create a mime-message that will render HTML in popular
MUAs, text in better ones"""
import MimeWriter
import mimetools
import cStringIO

out = cStringIO.StringIO() # output buffer for our message
htmlin = cStringIO.StringIO(html)
txtin = cStringIO.StringIO(text)

writer = MimeWriter.MimeWriter(out)
# set up some basic headers... we put subject here
# because smtplib.sendmail expects it to be in the
# message body
#
writer.addheader("Subject", subject)
writer.addheader("MIME-Version", "1.0")
#
# start the multipart section of the message
# multipart/alternative seems to work better
# on some MUAs than multipart/mixed
#
writer.startmultipartbody("alternative")
writer.flushheaders()
#
# the plain text section
#
subpart = writer.nextpart()
subpart.addheader("Content-Transfer-Encoding", "quoted-printable")
pout = subpart.startbody("text/plain", [("charset", 'us-ascii')])
mimetools.encode(txtin, pout, 'quoted-printable')
txtin.close()
#
# start the html subpart of the message
#
subpart = writer.nextpart()
subpart.addheader("Content-Transfer-Encoding", "quoted-printable")
#
# returns us a file-ish object we can write to
#
pout = subpart.startbody("text/html", [("charset", 'us-ascii')])
mimetools.encode(htmlin, pout, 'quoted-printable')
htmlin.close()
#
# Now that we're done, close our writer and
# return the message body
#
writer.lastpart()
msg = out.getvalue()
out.close()
return msg

if __name__=="__main__":
import smtplib
from time import *

f = open("mssg.html", 'r')
html = f.read()
f.close()
f = open("mssg.txt", 'r')
text = f.read()
f.close()
subject = "subject)"

f = open("temp.txt", 'r')
RECIPIENTS = []
for line in f:
RECIPIENTS.append(line.strip())
f.close()
for i in RECIPIENTS:
if len(i) == 0:
RECIPIENTS.remove(i)
print "Number of recipients: ", len(RECIPIENTS)

SENDER = '(e-mail address removed)'
print "Generating message..."
mssg = createhtmlmail(html, text, subject)
print "Opening session"
session = smtplib.SMTP("localhost")
print "Sending email"
offset = 0
blocksize = 20 # to send emails in blocks of 20, so I don't
bomb out my server
while offset*blocksize < len(RECIPIENTS):
print "Sending message ", offset*blocksize, " - ",
(offset+1)*blocksize, "of ", len(RECIPIENTS), "..."
smtpresult =
session.sendmail(SENDER,RECIPIENTS[offset*blocksize:(offset+1)*blocksize],mssg)
sleep(30)
offset += 1
## smtpresult = session.sendmail(SENDER, RECIPIENTS, mssg)

if smtpresult:
errstr = ""
for recip in smtpresult.keys():
errstr = """Could not delivery mail to: %s
Server said: %s
%s
%s""" % (recip, smtpresult[recip][0], smtpresult[recip][1], errstr)
raise smtplib.SMTPException, errstr
session.quit()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top