I've found a solution for sending multi-part emails (text & html) and
even embedded images.
In your controller (or a custom module)
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
#helper function to build a multi-part mime email
def _multipart_email(sender, recipient, subject, message_text,
#Taken from http://code.activestate.com/recipes/473810/
# Define these once; use them twice!
strFrom = sender
strTo = recipient
# Create the root message and fill in the from, to, and subject
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = subject
msgRoot['From'] = strFrom
msgRoot['To'] = strTo
msgRoot.preamble = 'This is a multi-part message in MIME format.'
# Encapsulate the plain and HTML versions of the message body in
# 'alternative' part, so message agents can decide which they want
msgAlternative = MIMEMultipart('alternative')
msgText = MIMEText(message_text)
# We reference the image in the IMG SRC attribute by the ID we
give it below
msgText = MIMEText(message_html, 'html')
# This example assumes the image is in the current directory
or you've passed the full path
fp = open(attach_image, 'rb')
msgImage = MIMEImage(fp.read())
#you can refer to this image as <img src="cid:image1"> in your
# Define the image's ID as referenced above
In your controller:
#visit this page and you'll get an email
host = 'smtp.gmail.com
' #your mail server
port = 587 #port if non-standard
username = 'your_username'
password = 'your_password'
server = smtplib.SMTP(host, port)
context=dict(name='John Doe', ship_date='July 15,
#use web2py views to get an html and plain text version of the
email_template.html',context) #template for the html version
message_text = response.render('default/
email_template.txt',context) #template for the text only version
sender = 'yo...@NOTSPAMMING.com'
recipient = 'cust...@host.com
subject = "We've shipped your order"
#build the multi-part MIME message
message = _multipart_email(sender, recipient, subject,
message_text, message_html, 'image.jpg')
#and send it
server.sendmail(sender, recipient, message.as_string())
Perhaps someone else here can use this and maybe even integrate it
into web2py in a more standard way.
On Jul 11, 3:22 am, kralin <andrea.pierle...@gmail.com
> yes, I've already tryed this way, after looking at tools.py
> maybe I'm doing something wrong, I'm going to recheck...
> On 11 Lug, 00:46, mdipierro <mdipie...@cs.depaul.edu
> > import smtplib
> > server = smtplib.SMTP(host, port)
> > server.ehlo()
> > server.starttls()
> > server.ehlo()
> > server.login(username, password)
> > server.sendmail(sender, to, msg)
> > server.quit()
> > look into the docs for datils. Mind that the docs say .ehlo and .helo
> > are the same. They are not. The latter does not work with starttls
> > On Jul 10, 4:46 pm, kralin <andrea.pierle...@gmail.com
> > > ok, so smtplib should work instead of Mail?
> > > On 10 Lug, 23:39, mdipierro <mdipie...@cs.depaul.edu
> > > > Here is the problem. The current implementation of Mail is designed to
> > > > be cross platform, i.e. work on GAE. It has the same API as GAE.
> > > > Unless we figure out how to send MIMEemailmessages on GAE, if you
> > > > want to send MIME you should not use Mail native SMTP.
> > > > On Jul 10, 4:31 pm, kralin <andrea.pierle...@gmail.com
> > > > > I've also tried to use MIME to encode both thehtml, and an
> > > > > alternative text/htmlmessage, however all the encoding goes after
> > > > > the double newline
> > > > > and is not interpreted as an header.
> > > > > so Yarko are you saying that by doing send(message='hello message')
> > > > > instead of send(to=['...@me.com
'], message='hello message')
> > > > > it should worrk by including the correct headers in the message?
> > > > > On 10 Lug, 22:15, mdipierro <mdipie...@cs.depaul.edu
> > > > > > While this can be and something like this can be added it is my
> > > > > > understanding that you do not need to change the header to sendhtml
> > > > > > emails. You need to use the MIME encoding and that should be
> > > > > > transparent to what mail.send does now. If I am wrong please provide
> > > > > > an example of how to change the headers to send a MIME encoded
> > > > > > message.
> > > > > > On Jul 10, 1:31 pm, kralin <andrea.pierle...@gmail.com
> > > > > > > I spent the whole afternoon trying to send anHTMLformattedemail
> > > > > > > with web2py.
> > > > > > > while I cannote use smtplib directly, cause it seems not to work, and
> > > > > > > I think this should be "normal" in the framework.
> > > > > > > however in the current release (Version 1.65.0 (2009-07-01 12:16:25))
> > > > > > > it is not possible to alter the content-type, or any other header for
> > > > > > > theemailusing the gluon.tools.Mail class.