471,616 Members | 1,447 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,616 software developers and data experts.

Adding sender name to email

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 = 'm***@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()

Sep 1 '06 #1
0 1650

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by serge calderara | last post: by
3 posts views Thread by Jim Heavey | last post: by
3 posts views Thread by Ankit Aneja | last post: by
1 post views Thread by Manuel Canas | last post: by
1 post views Thread by XIAOLAOHU | last post: by
1 post views Thread by ZEDKYRIE | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.