Sending an email with a binary attachment

Anthony Papillion anthony at cajuntechie.org
Tue Mar 1 23:29:08 EST 2016


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




More information about the Python-list mailing list