Getting "empty" attachment with smtplib

T

Tobiah

I've been sending an email blast out with smtplib and
it's been working fine. I'm attaching an html doc with

msg.attach(MIMEText(email, 'html'))

and it displays fine. Now I need to attach a .pdf
doc, but Thunderbird complains that the attachment
is empty. When I view the source of the email, the
headers look ok to me, and a large base64 looking
mess follows:

--===============0152408622==
Content-Type: application/pdf
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="tics.pdf"

JVBERi0xLjYNJeLjz9MNCjE0IDAgb2JqDTw8L0xpbmVhcml6ZWQgMS9MIDExNzk1My9PIDE2L0Ug
MTEyNjE3L04gMS9UIDExNzY0My9IIFsgNjA2IDI1M10+Pg1lbmRvYmoNICAgICAgICAgICAgICAg
DQo2MSAwIG9iag08PC9EZWNvZGVQYXJtczw8L0NvbHVtbnMgNS9QcmVkaWN0b3IgMTI+Pi9GaWx0
ZXIvRmxhdGVEZWNvZGUvSURbPDg4RkMxMTM2QjQ3RDhEQzRFMjkxQkEzRDJGNEIyODBBPjxGRTNC
RkM3MjNFMDg3QzRCQUEyNTUzMkM5NEI5QjNCOT5dL0luZGV4WzE0IDc4XS9JbmZvIDEzIDAgUi9M

and so on. I've tried a few recipes, and this is the one I'm trying now:

pdf = MIMEApplication(pdf_data, 'pdf')
pdf.add_header('Content-Disposition','attachment', filename = 'tics.pdf')
msg.attach(pdf)

Any help is appreciated. Also, if anyone has a working recipe, I'd like to
see it.

Thanks!

Tobiah
 
T

Tobiah

I just found out that the attachment works fine
when I read the mail from the gmail website. Thunderbird
complains that the attachment is empty.

Thanks,

Toby
 
S

Steven D'Aprano

I just found out that the attachment works fine when I read the mail
from the gmail website. Thunderbird complains that the attachment is
empty.

I suggest that you use Thunderbird to send yourself an email with a PDF
attachment, then inspect how it is attached. How does that differ from
the one you are sending from Python?

When I try sending a PDF from mutt, I get:

Content-Type: application/pdf
Content-Disposition: attachment; filename="foo.pdf"
Content-Transfer-Encoding: base64

I haven't tried opening that in Thunderbird, but I expect it should be
okay.

The only difference I see between what you are sending and mutt is that
mutt doesn't include a MIME-version line. Perhaps Thunderbird doesn't
like that?

Wait... here's another thought. Perhaps Thunderbird doesn't know how to
display the PDF file, and is giving a bogus error message? Can your
Thunderbird display PDF files at all? Does it succeed when you use a
different PDF file? What if you send a JPEG?
 
D

Dieter Maurer

Tobiah said:
I just found out that the attachment works fine
when I read the mail from the gmail website. Thunderbird
complains that the attachment is empty.

The MIME standard (a set of RFCs) specifies how valid messages
with attachments should look like.

Fetch the mail (unprocessed if possible) and look at its
structure. If it is conformant to the MIME standard, then
"Thunderbird" made a mistake; otherwise, something went wrong
with the message construction.

I can already say that "smtplib" is not to blame. It is (mostly) unconcerned
with the internal structure of the message -- and by itself
will not empty attachments.
 
T

Tobiah

I can already say that "smtplib" is not to blame. It is (mostly) unconcerned
with the internal structure of the message -- and by itself
will not empty attachments.

On the advice of a co-worker, I tried using web2py's gluon.tools.Mail. It
was easier to accomplish the attachment, and Thunderbird opened the .pdf
just fine.

Thanks for the suggestions.

Tobiah
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top