Adding sender name to email

gillespie.amanda at gmail.com gillespie.amanda at gmail.com
Fri Sep 1 06:26:12 EDT 2006


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 = 'mail at mail.com'
        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()




More information about the Python-list mailing list