Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #103782

Re: Sending an email with a binary attachment

From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: Sending an email with a binary attachment
Date 2016-03-01 19:03 +1100
Message-ID <mailman.59.1456819439.20602.python-list@python.org> (permalink)
References <mailman.0.1456733421.20602.python-list@python.org> <djjc8gFl1hoU1@mid.individual.net> <CAPTjJmq-Qj9UEg0MNfAWo0CivMcO449=gSA913p4=sN+dp9V8A@mail.gmail.com> <56D54B99.2020206@cajuntechie.org>

Show all headers | View raw


On Tue, Mar 1, 2016 at 6:58 PM, Anthony Papillion
<anthony@cajuntechie.org> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA512
>
> On 02/29/2016 11:13 AM, Chris Angelico wrote:
>> On Tue, Mar 1, 2016 at 4:08 AM, Peter Pearson
>> <pkpearson@nowhere.invalid> wrote:
>>> try: smtp.sendmail(message['From'], message['To'],
>>> message.as_string()) except: print "Message sending has failed"
>>> sys.exit(1) print "Message sending was successful" sys.exit(0)
>>>
>>
>> This is the problem, right here. Replace this code with:
>>
>> smtp.sendmail(message['From'], message['To'], message.as_string())
>
> Hmm, I'm a bit confused. Are you saying that the problem is that I'm
> enclosing the code in a Try/Except block? Besides that, I don't see
> anything different. If it's the Try/Except block, how do I catch the
> exception it might generate if I'm not using the exception block?
>

That's exactly the difference. Why do you need to catch the exception?
All you're doing is destroying all the information, rendering it down
to a blunt "has failed".

We've had several threads touching on this, recently. I'm going to say
this in what might be taken as a rude way, but the emphasis is
necessary:

** Folks, *stop catching exceptions* just to print failure messages
and exit. You are shooting yourselves in the foot. **

You should catch exceptions if you can actually handle them, but if
all you're doing is printing out a fixed message and aborting, delete
that code. Less code AND a better result.

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Sending an email with a binary attachment Anthony Papillion <anthony@cajuntechie.org> - 2016-02-29 02:10 -0600
  Re: Sending an email with a binary attachment Peter Pearson <pkpearson@nowhere.invalid> - 2016-02-29 17:08 +0000
    Re: Sending an email with a binary attachment Chris Angelico <rosuav@gmail.com> - 2016-03-01 04:13 +1100
    Re: Sending an email with a binary attachment Grant Edwards <invalid@invalid.invalid> - 2016-02-29 18:22 +0000
    Re: Sending an email with a binary attachment Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-02-29 20:43 -0500
    Re: Sending an email with a binary attachment Anthony Papillion <anthony@cajuntechie.org> - 2016-03-01 01:58 -0600
    Re: Sending an email with a binary attachment Chris Angelico <rosuav@gmail.com> - 2016-03-01 19:03 +1100
    Re: Sending an email with a binary attachment Anthony Papillion <anthony@cajuntechie.org> - 2016-03-01 22:29 -0600

csiph-web