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


Groups > comp.lang.python > #103836

Re: Sending an email with a binary attachment

From Anthony Papillion <anthony@cajuntechie.org>
Newsgroups comp.lang.python
Subject Re: Sending an email with a binary attachment
Date 2016-03-01 22:29 -0600
Message-ID <mailman.91.1456892958.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> <CAPTjJmp473VXUERM63KghTw4w4BN3L_gSRWuRUHrzWbERdVZow@mail.gmail.com>

Show all headers | View raw


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

On 03/01/2016 02:03 AM, Chris Angelico wrote:
> 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".

Mostly, I catch exceptions for two reasons: 1) because it's a habit
that I've developed over the years and 2) I think unhandled exceptions
make things look ugly. Mostly, I catch them from habit though.

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

You make some very good points. It's going to take me some time to not
catch exceptions by knee-jerk but I can see how doing so can cripple
code and make debugging much harder. Thanks for the pointers.

Anthony


-----BEGIN PGP SIGNATURE-----

iQIcBAEBCgAGBQJW1mwTAAoJEAKK33RTsEsV0L4P/jvRCX7w+8iqzlFub0CS35C6
KtuFXLEh+evKGhBecgToCA9eutuvCltknCxJz/Yyd56+QFsze1HHdDWGakuOP/1x
gOwzZKr1vsjD4eMkoRRokIVkg437yOju0OReUOATKpYGgwnB6xW9RbOLwHRftXfa
pmxg5k2KCBZ1omVLQ1BQcvM48Vi5J4k6IlFAVyM/L3Dzsyj9E1CtJ/VarTwkmAOf
RbrBV7EH/k1ELM6yWsm0P00zhQkwZTdKt+Y3OGj7WaYoZXk7D3Q8wJqOJrHgInCr
/JjbjX8yHtcVVaIRPKGVGt5PGNDdGvkmI5mJPXL+Io0k8faA4QLqjdCTFniyJ2t3
6HprovGQOJs64WN9RshcCwncJcWLC1wcLWZhZOj9nNZawTM6pWEDFpn1zmg6/lqu
DmqhEeudjUtCjRJZr9xey47JJRRkjUrh/g1+VRW+aUfeuIc7xA6Nw7qvf3PqT6tH
8CtVwK9O2sHQ4y8lzAK3vCYY3Lw34qdO2zBC0ycxhMPbk1BqcL5U4WOTDZD/9H3h
sh0E19pLYsAtoJpF2tnB2PcJaIWA9Zofz5/K6+5fs5B1wVW34Nu8wz6LMhUR1qSW
yJH9Wv8Oznk99qiYUiOiduqGKwLM4+Fg0xYAX1muGNRvLfcYuGDFkyYhdZn9f7dM
QdmK/dIaGT9l45hjDQTu
=RlhP
-----END PGP SIGNATURE-----

Back to comp.lang.python | Previous | NextPrevious 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