473,320 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

email modules and attachments that aren't there

Hello all,

I have written a short function, based on a recipe in the Python
Cookbook, that sends an e-mail. The function takes arguments that
define who the e-mail is to, from, the subject, the body and an optional
list of attachments.

The function works also perfectly, bar one slight problem. If you
attempt to send an e-mail with just a body and no attachments, the
receiving client still thinks that there is an attachment (so far tested
in Mozilla Thunderbird and the Yahoo! webmail client). Although this
clearly isn't a major problem, it is irritating and I am hoping to use
my code at work. Obviously I can't be sending out badly formed e-mails
to my clients.

I can't for the life of me work out why. I have compared my code to
every example that I can find in the Python documentation, on the
archives of this newsgroup and of the Python Tutor list, and one or two
random searches but can't see what is happening. Any advice or
suggestions would be welcome.

Thank you for your help,

Russell Bungay
--
The Duck Quacks:
http://www-users.york.ac.uk/~rb502/ - Homepage
http://www-users.york.ac.uk/~rb502/blog/quack.shtml - Blog
http://www.flickr.com/photos/lsnduck/ - Photos

Code:

def sendEmail(msg_to, msg_from, msg_subject, message, attachments=[]):

main_msg = email.Message.Message()
main_msg['To'] = ', '.join(msg_to)
main_msg['From'] = msg_from
main_msg['Subject'] = msg_subject
main_msg['Date'] = email.Utils.formatdate(localtime=1)
main_msg['Message-ID'] = email.Utils.make_msgid()
main_msg['Mime-version'] = '1.0'
main_msg['Content-type'] = 'Multipart/mixed'
main_msg.preamble = 'Mime message\n'
main_msg.epilogue = ''

body_encoded = quopri.encodestring(message, 1)
body_msg = email.Message.Message()
body_msg.add_header('Content-type', 'text/plain')
body_msg.add_header('Content-transfer-encoding', 'quoted-printable')
body_msg.set_payload(body_encoded)
main_msg.attach(body_msg)

for attachment in attachments:

content_type, ignored = mimetypes.guess_type(attachment)
if content_type == None:
content_type = 'application/octet-stream'
contents_encoded = cStingIO.StringIO()
attach_file = open(attachment, 'rb')
main_type = content_type[:content_type.find('/')]
if main_type == 'text':
cte = 'quoted-printable'
quopri.encode(attach_file, contents_encoded, 1)
else:
cte = 'base64'
base64.encode(attach_file, contents_encoded)
attach_file.close()

sub_msg = email.Message.Message()
sub_msg.add_header('Content-type', content_type, name=attachment)
sub_msg.add_header('Content-transfer-encoding', cte)
sub_msg.set_payload(contents_encoded.getvalue())
main_msg.attach(sub_msg)

smtp = smtplib.SMTP(server)
smtpfail = smtp.sendmail(msg_from, ', '.join(msg_to),
main_msg.as_string())
smtp.quit()
Jan 9 '06 #1
4 1917
Russell Bungay wrote:
Hello all,

I have written a short function, based on a recipe in the Python
Cookbook, that sends an e-mail. The function takes arguments that
define who the e-mail is to, from, the subject, the body and an optional
list of attachments.

The function works also perfectly, bar one slight problem. If you
attempt to send an e-mail with just a body and no attachments, the
receiving client still thinks that there is an attachment (so far tested
in Mozilla Thunderbird and the Yahoo! webmail client).
<snip>
Code:

def sendEmail(msg_to, msg_from, msg_subject, message, attachments=[]):

main_msg = email.Message.Message()
main_msg['To'] = ', '.join(msg_to)
main_msg['From'] = msg_from
main_msg['Subject'] = msg_subject
main_msg['Date'] = email.Utils.formatdate(localtime=1)
main_msg['Message-ID'] = email.Utils.make_msgid()
main_msg['Mime-version'] = '1.0'
main_msg['Content-type'] = 'Multipart/mixed'
main_msg.preamble = 'Mime message\n'
main_msg.epilogue = ''


Would it be the 'Content-Type' header? I've no expertise in this, but
doesn't 'multipart' mean 'has attachments'?

Gerard

Jan 9 '06 #2
Hello,
main_msg['Content-type'] = 'Multipart/mixed'

Would it be the 'Content-Type' header? I've no expertise in this, but
doesn't 'multipart' mean 'has attachments'?


Brilliant, thank you. A swift test on the number of attachments and
changing the header suitably does the job.

Thank you for your help,

Russell
--
The Duck Quacks:
http://www-users.york.ac.uk/~rb502/ - Homepage
http://www-users.york.ac.uk/~rb502/blog/quack.shtml - Blog
http://www.flickr.com/photos/lsnduck/ - Photos
Jan 10 '06 #3
Hello,
main_msg['Content-type'] = 'Multipart/mixed'

Would it be the 'Content-Type' header? I've no expertise in this, but
doesn't 'multipart' mean 'has attachments'?

Brilliant, thank you. A swift test on the number of attachments and
changing the header suitably does the job.


That isn't quite all there is to it, the e-mail construction needs a
slight change as well. Roughly working code below.

Ta,

Russell

Code:

def sendEmail(msg_to, msg_from, msg_subject, message, attachments=[]):

main_msg = email.Message.Message()
main_msg['To'] = ', '.join(msg_to)
main_msg['From'] = msg_from
main_msg['Subject'] = msg_subject
main_msg['Date'] = email.Utils.formatdate(localtime=1)
main_msg['Message-ID'] = email.Utils.make_msgid()
main_msg['Mime-version'] = '1.0'
main_msg.preamble = 'Mime message\n'
main_msg.epilogue = ''

body_encoded = quopri.encodestring(message, 1)

if len(attachments) <> 0:
main_msg['Content-type'] = 'Multipart/mixed'
body_msg = email.Message.Message()
body_msg.add_header('Content-type', 'text/plain')
body_msg.add_header('Content-transfer-encoding',
'quoted-printable')
body_msg.set_payload(body_encoded)
main_msg.attach(body_msg)
for attachment in attachments:
content_type, ignored = mimetypes.guess_type(attachment)
if content_type == None:
content_type = 'application/octet-stream'
contents_encoded = cStringIO.StringIO()
attach_file = open(attachment, 'rb')
main_type = content_type[:content_type.find('/')]
if main_type == 'text':
cte = 'quoted-printable'
quopri.encode(attach_file, contents_encoded, 1)
else:
cte = 'base64'
base64.encode(attach_file, contents_encoded)
attach_file.close()

sub_msg = email.Message.Message()
sub_msg.add_header('Content-type', content_type, name=attachment)
sub_msg.add_header('Content-transfer-encoding', cte)
sub_msg.set_payload(contents_encoded.getvalue())
main_msg.attach(sub_msg)

else:
main_msg['Content-type'] = 'text/plain'
main_msg['Content-transfer-encoding'] = 'quoted-printable'
main_msg.set_payload(body_encoded)

smtp = smtplib.SMTP('server')
smtpfail = smtp.sendmail(msg_from, ', '.join(msg_to),
main_msg.as_string())
smtp.quit()
Jan 10 '06 #4
Russell Bungay wrote:
for attachment in attachments:
<snip contents of for loop> sub_msg = email.Message.Message()
sub_msg.add_header('Content-type', content_type, name=attachment)
sub_msg.add_header('Content-transfer-encoding', cte)
sub_msg.set_payload(contents_encoded.getvalue())
main_msg.attach(sub_msg)


These lines should of course be within the for, not outside it. Apologies.

Russell
Jan 10 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Paul Schmidt | last post by:
Dear list: I am new to python, and I am trying to figure out the short answer on something. I want to open a POP3 mailbox, read the enclosed mail using the POP3 module, , and then process it...
3
by: LutherRevisited | last post by:
Is there a way I can put a message together without having to download any attachments there may be at the same time. I'm not having any problems dealing with attachments, but the way I'm doing...
2
by: brettk | last post by:
Hello All, Here's what I'm trying to do: I need to connect to a pop3 server, download all messages, and copy all of the attachments into a specific directory. The actual email message is...
0
by: YYZ | last post by:
Problem: I need to allow my users to drag an email out of Outlook and drop it on a winform -- from there, I need to get the text of that email (not just the sender and subject) -- from there I'll...
10
by: OdAwG | last post by:
Hello All, Is it possible to send an email from Access? I found a Microsoft article on how to do this but I keep getting an error "RUNTIME ERROR 438" -- Object doesn't support this property or...
1
by: darklink64 | last post by:
Hi, I've recently set up a database, but have not experience of VBA and need to find a way of solving this problem. Basically, I would like to make a form with two buttons. On clicking one...
2
by: oyster | last post by:
I find that the existing email moudle is some hard for me to understand, especially the part of how to set the CC, BCC and attach the files. Is there any more easy one like this p-code? import...
2
by: David Veeneman | last post by:
My web app creates several temporary files, then attaches them to an email that it sends out. When the app attches the documents to the email, .NET locks the files. My problem is that they aren't...
3
by: =?Utf-8?B?TWlrZQ==?= | last post by:
Hi, I'm using the bellow code to send emails in a Website that I uploaded in one of my client's server where it has SMTP installed, BUT some users are receiving email some others aren't !!?So,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.