Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Anthony Papillion Newsgroups: comp.lang.python Subject: Re: Sending an email with a binary attachment Date: Tue, 1 Mar 2016 22:29:08 -0600 Lines: 67 Message-ID: References: <56D54B99.2020206@cajuntechie.org> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de lvc9aV48WJ3kGccef16Hcgig6MWtTB7y48dZ+0iEmwpQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'skip:p 60': 0.05; 'except:': 0.07; 'pgp': 0.09; 'skip:g 60': 0.09; 'unhandled': 0.09; 'exception': 0.13; 'message-----': 0.15; 'result.': 0.15; '"message': 0.16; '-----begin': 0.16; '-----end': 0.16; '2016': 0.16; 'exception?': 0.16; 'from:addr:anthony': 0.16; 'hash:': 0.16; 'pointers.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'skip:6 60': 0.16; 'skip:d 60': 0.16; 'sys.exit(0)': 0.16; 'sys.exit(1)': 0.16; 'try/except': 0.16; 'ugly.': 0.16; 'wrote:': 0.16; 'debugging': 0.18; 'try:': 0.18; '>>>': 0.20; 'saying': 0.22; 'exceptions': 0.22; 'am,': 0.23; 'code.': 0.23; 'bit': 0.23; 'header:In-Reply-To:1': 0.24; 'signed': 0.24; "i've": 0.25; 'header:User-Agent:1': 0.26; 'chris': 0.26; 'block,': 0.29; 'skip:i 60': 0.29; 'skip:k 60': 0.29; 'skip:q 20': 0.29; 'skip:r 60': 0.29; 'skip:y 60': 0.29; 'subject:Sending': 0.29; "i'm": 0.30; 'print': 0.30; 'code': 0.30; 'fixed': 0.31; 'skip:s 30': 0.31; 'problem': 0.33; 'subject:attachment': 0.33; 'though.': 0.33; 'tue,': 0.34; 'handle': 0.34; 'received:10.0': 0.34; 'that,': 0.34; 'besides': 0.35; 'replace': 0.35; 'skip:s 60': 0.35; 'but': 0.36; 'should': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'two': 0.37; 'thanks': 0.37; 'things': 0.38; 'doing': 0.38; 'delete': 0.38; 'anything': 0.38; 'why': 0.39; 'to:addr:python.org': 0.40; 'subject:with': 0.40; 'some': 0.40; 'information,': 0.61; 'here.': 0.62; 'mar': 0.65; 'developed': 0.66; 'rendering': 0.76; 'cripple': 0.84; 'difference.': 0.84; 'reasons:': 0.84; 'received:10.0.2': 0.84; 'different.': 0.91; 'habit': 0.91 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.5.1 In-Reply-To: X-Zoho-Virus-Status: 1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:103836 -----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 > 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 >>> 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. > > 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-----