Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: Sending an email with a binary attachment Date: Tue, 1 Mar 2016 19:03:50 +1100 Lines: 39 Message-ID: References: <56D54B99.2020206@cajuntechie.org> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de qnrt40e3HEvIeESphUY+9Q8ciGjFrzuMK6IgGQTqTVQA== 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; 'except:': 0.07; 'cc:addr :python-list': 0.09; 'exit.': 0.09; 'pgp': 0.09; 'exception': 0.13; 'message-----': 0.15; 'result.': 0.15; '"message': 0.16; ' -----begin': 0.16; '2016': 0.16; 'exception?': 0.16; 'folks,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'hash:': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'recently.': 0.16; 'shooting': 0.16; 'sys.exit(0)': 0.16; 'sys.exit(1)': 0.16; 'threads': 0.16; 'try/except': 0.16; 'wrote:': 0.16; 'try:': 0.18; '>>>': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 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; 'chris': 0.26; 'message-id:@mail.gmail.com': 0.27; 'block,': 0.29; 'catching': 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; 'tue,': 0.34; 'handle': 0.34; 'that,': 0.34; 'received:google.com': 0.35; 'besides': 0.35; 'replace': 0.35; 'but': 0.36; 'should': 0.36; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'say': 0.37; 'received:209.85.213': 0.37; 'doing': 0.38; 'received:209': 0.38; 'delete': 0.38; 'anything': 0.38; 'several': 0.38; 'why': 0.39; 'subject:with': 0.40; 'information,': 0.61; 'here.': 0.62; 'mar': 0.65; 'rendering': 0.76; 'chrisa': 0.84; 'difference.': 0.84; 'touching': 0.84; 'to:none': 0.91; 'different.': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc; bh=cIihWuzxqDhUAs+kIr7NWzgsTdey6ndn9AObjmYSTgE=; b=F54OEQDKYeyD3szbAHbvTXOY6eLdEAGTewOL+90j5zvXVo5OxdZeoZ4+Yl4YaGfi0f bueu23f1sAxW0HX3vsSlq+nlWc71kv2fIPH6/Zak90bL1MymDBZrN6ROodcIYNqxdUcc 80A0o9QLo8Ho5XCAdVDib5bOOLKfctObeKkVk208Q24Tj2E7vzdStnCF4mWdDHWM0F+C CmFiEWR8CUgt/Y8yNHzAG6TwwBlqPedUohoXeM+pjI22MuFI3VT2HdZ3NghIivi5uVX/ y2vJhJCbDYAOo0nUrbdEJM9SS6dfZI7K042btRgtMz1GBKEBqZ8xBgMPYDhJwM7dKlLl Nl3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=cIihWuzxqDhUAs+kIr7NWzgsTdey6ndn9AObjmYSTgE=; b=kGkOHP9xPhjC11iEQ6p982zEGwH2DyXG9bVw94/PISic8Y3HoTb0vkNiDTbSKEAXo+ B2U6XW6BaPwF6bStS+Dewww8Ubx2cRgTsFUqHjvCF8pxWOexU/Q9eoKsislS2is9hl21 FlTRjJIyAM1MxTrjkTGOBJSh65xvJIaiBHJdkLEZi1SGAyzk8xLHgoHQWYnwKD+Ruqex 1iBt43Udn5zI6mOziN2vR3UD4JOIEI2dlst7vp7KA2M1VB1Heg8IK+VU+E+K+3Elwc0Z PJ6AXkDNH/iumRUaTo1dcc0cysxxJIz2Cqgkpz8Z4v/9Dj0ZuKNlxDflkYOI7EgOYlhc SB9g== X-Gm-Message-State: AD7BkJKdEHiSc65c7+SHDXNn4gNQAwuFo/cZdWYAJrvoS6J6Sn4gsL47GCNo4FLMjj5lsNHGSZ0+JJgfIqh/tg== X-Received: by 10.50.43.168 with SMTP id x8mr2275403igl.92.1456819431034; Tue, 01 Mar 2016 00:03:51 -0800 (PST) In-Reply-To: <56D54B99.2020206@cajuntechie.org> 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:103782 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". 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