Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Getting "empty" attachment with smtplib (http://www.velocityreviews.com/forums/t954515-getting-empty-attachment-with-smtplib.html)

Tobiah 11-14-2012 05:51 PM

Getting "empty" attachment with smtplib
 
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"

JVBERi0xLjYNJeLjz9MNCjE0IDAgb2JqDTw8L0xpbmVhcml6ZW QgMS9MIDExNzk1My9PIDE2L0Ug
MTEyNjE3L04gMS9UIDExNzY0My9IIFsgNjA2IDI1M10+Pg1lbm RvYmoNICAgICAgICAgICAgICAg
DQo2MSAwIG9iag08PC9EZWNvZGVQYXJtczw8L0NvbHVtbnMgNS 9QcmVkaWN0b3IgMTI+Pi9GaWx0
ZXIvRmxhdGVEZWNvZGUvSURbPDg4RkMxMTM2QjQ3RDhEQzRFMj kxQkEzRDJGNEIyODBBPjxGRTNC
RkM3MjNFMDg3QzRCQUEyNTUzMkM5NEI5QjNCOT5dL0luZGV4Wz E0IDc4XS9JbmZvIDEzIDAgUi9M

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

Tobiah 11-14-2012 05:58 PM

Re: Getting "empty" attachment with smtplib
 
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

On 11/14/2012 09:51 AM, Tobiah wrote:
> 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"
>
> JVBERi0xLjYNJeLjz9MNCjE0IDAgb2JqDTw8L0xpbmVhcml6ZW QgMS9MIDExNzk1My9PIDE2L0Ug
> MTEyNjE3L04gMS9UIDExNzY0My9IIFsgNjA2IDI1M10+Pg1lbm RvYmoNICAgICAgICAgICAgICAg
> DQo2MSAwIG9iag08PC9EZWNvZGVQYXJtczw8L0NvbHVtbnMgNS 9QcmVkaWN0b3IgMTI+Pi9GaWx0
> ZXIvRmxhdGVEZWNvZGUvSURbPDg4RkMxMTM2QjQ3RDhEQzRFMj kxQkEzRDJGNEIyODBBPjxGRTNC
> RkM3MjNFMDg3QzRCQUEyNTUzMkM5NEI5QjNCOT5dL0luZGV4Wz E0IDc4XS9JbmZvIDEzIDAgUi9M
>
> 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



Steven D'Aprano 11-15-2012 04:54 AM

Re: Getting "empty" attachment with smtplib
 
On Wed, 14 Nov 2012 09:58:30 -0800, Tobiah wrote:

> 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?



--
Steven

Dieter Maurer 11-15-2012 07:37 AM

Re: Getting "empty" attachment with smtplib
 
Tobiah <toby@tobiah.org> writes:

> 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.


Tobiah 11-15-2012 05:26 PM

Re: Getting "empty" attachment with smtplib
 
> 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



All times are GMT. The time now is 03:16 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.