Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #103691 > unrolled thread
| Started by | Anthony Papillion <anthony@cajuntechie.org> |
|---|---|
| First post | 2016-02-29 02:10 -0600 |
| Last post | 2016-03-01 22:29 -0600 |
| Articles | 8 — 5 participants |
Back to article view | Back to comp.lang.python
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
| From | Anthony Papillion <anthony@cajuntechie.org> |
|---|---|
| Date | 2016-02-29 02:10 -0600 |
| Subject | Sending an email with a binary attachment |
| Message-ID | <mailman.0.1456733421.20602.python-list@python.org> |
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA512 Hello Everyone, I've been trying to write a /very/ simple script that will email a binary file to an email address when run. Pretty simple. I've done quite a bit of research and have finally settled on the following code: http://pastebin.com/sryj98wW For some reason though, sending mail is failing every time. I've made sure that the password is correct (which seems to be the most usual error). Still, I just can't get it to work. Can someone take a look at this code and give me some advice? Thanks! Anthony -----BEGIN PGP SIGNATURE----- iQIcBAEBCgAGBQJW0/zYAAoJEAKK33RTsEsVH5YP/jisLkuK4SAD6E06dO/czn1V bO1QlzqTje8giXUWGyVeGS9MHAA1S2TlHc/rp01/DHTt1iYT50qGIaPxzUxBTMEX mBCFFUSgg6LjkY+9b0c+o4OmviZlX7/KA6wT9IpFEG0l86Uj+z39rcBHm+GauiX+ IgMS5lWXfPmIEwiKzByMduxEgfj6kQW6PhkR5hTmOvd3sbILR91U9NiigrUD1ukM +eN4LnKVu5Qpyb8X34Ze0NF7tiMr7tTQyBIq6C6ORGR/IPri407Ro8ACHBd55xZV QxHjFVemms9s7ala0aoBcpRKr4PG35Ebm+7CcZaDj0fdk11mRvpqCZtGfodw/+sL Crrxoz11hC//DHslZrEuNlJHgRqnsw+abKG8jIaJCrIW1f9BiyiWse+RRwbpN8IY 1C0gD4E1N3dUqGFrhyD/Xuy0tkXWpfiYhcLJQY+j7exrf00s+ASUTrd92lDCzxW4 pwuPZYN6nXY6dh9q72fgoEzqU0eDIzSgX99fODcxqp1L2ieczWMMajlWQJrJEcUe UgxB8IvlvuD3NpzZXisEG8ZzqhZT6GOurHGbBusAW2M9zaqImb4qGEid7HS4BL/c Kj7PspXz+8gD3SKZ/dOBdFlKK1KM1utekB0UnmBboZ9nXVhz5qVPaiVgjPunB5TG 1fMuuGiy2yl5pfTNvH8a =Ff+J -----END PGP SIGNATURE-----
[toc] | [next] | [standalone]
| From | Peter Pearson <pkpearson@nowhere.invalid> |
|---|---|
| Date | 2016-02-29 17:08 +0000 |
| Message-ID | <djjc8gFl1hoU1@mid.individual.net> |
| In reply to | #103691 |
On Mon, 29 Feb 2016 02:10:00 -0600, Anthony Papillion wrote:
[snip]
>
> http://pastebin.com/sryj98wW
To improve odds of your getting a response, let's get the code
into this thread. Here it is, minus the #! preamble:
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from smtplib import SMTP
import sys
FILE_TO_SEND = "/home/anthony/Desktop/passwords.kdb"
message = MIMEMultipart()
message['Subject'] = 'Password database update'
message['From'] = 'Database Update Script <xxx@cajuntechie.org'
message['reply-to'] = 'xxx@cajuntechie.org'
message['to'] = 'Me <xxx@cajuntechie.org'
message.preamble = 'Multipart massage.\n'
part = MIMEText("Attached is the latest update to the password database.")
message.attach(part)
part = MIMEApplication(open(FILE_TO_SEND, "rb").read())
part.add_header('Content-Disposition', 'attachment',
filename = FILE_TO_SEND)
message.attach(part)
smtp = SMTP("smtp.zoho.com", 587)
smtp.ehlo()
smtp.starttls()
smtp.login("xxxx@cajuntechie.org", "my_password")
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)
> For some reason though, sending mail is failing every time. I've made
> sure that the password is correct (which seems to be the most usual
> error).
>
> Still, I just can't get it to work. Can someone take a look at this
> code and give me some advice?
What exactly do you mean by "failing"? If there's an error message,
show it. If an exception is being generated, at least print some
information about the exception.
--
To email me, substitute nowhere->runbox, invalid->com.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2016-03-01 04:13 +1100 |
| Message-ID | <mailman.27.1456766046.20602.python-list@python.org> |
| In reply to | #103736 |
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())
ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Grant Edwards <invalid@invalid.invalid> |
|---|---|
| Date | 2016-02-29 18:22 +0000 |
| Message-ID | <nb2295$ciq$2@reader1.panix.com> |
| In reply to | #103736 |
On 2016-02-29, Peter Pearson <pkpearson@nowhere.invalid> wrote:
> On Mon, 29 Feb 2016 02:10:00 -0600, Anthony Papillion wrote:
>
>> For some reason though, sending mail is failing every time. I've made
>> sure that the password is correct (which seems to be the most usual
>> error).
>>
>> Still, I just can't get it to work. Can someone take a look at this
>> code and give me some advice?
>
> What exactly do you mean by "failing"? If there's an error message,
> show it. If an exception is being generated, at least print some
> information about the exception.
If you aren't getting any messages, then tell the smtp object you want some:
From TFM (https://docs.python.org/3/library/smtplib.html#smtp-objects):
An SMTP instance has the following methods:
SMTP.set_debuglevel(level)
Set the debug output level. A value of 1 or True for level
results in debug messages for connection and for all messages
sent to and received from the server. A value of 2 for level
results in these messages being timestamped.
If all else fails, fire up wireshark and watch the conversation
between the SMTP object and the server.
--
Grant Edwards grant.b.edwards Yow! Can you MAIL a BEAN
at CAKE?
gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2016-02-29 20:43 -0500 |
| Message-ID | <mailman.52.1456796603.20602.python-list@python.org> |
| In reply to | #103736 |
On 29 Feb 2016 17:08:32 GMT, Peter Pearson <pkpearson@nowhere.invalid>
declaimed the following:
>
>message.preamble = 'Multipart massage.\n'
>
I hope that's a transcription error...
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [next] | [standalone]
| From | Anthony Papillion <anthony@cajuntechie.org> |
|---|---|
| Date | 2016-03-01 01:58 -0600 |
| Message-ID | <mailman.58.1456818869.20602.python-list@python.org> |
| In reply to | #103736 |
-----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? Thanks, Anthony -----BEGIN PGP SIGNATURE----- iQIbBAEBCgAGBQJW1UuZAAoJEAKK33RTsEsVdfoP+N/xBIMouhhS1pqInvd9wrk2 jCfjmk7aHQQOdSvtQjXEqdNUu6nPmvX9VRzqvs881bUZAx31usL7VnyzJsL89pZl PPrXG3WzEDkMaWmfZ45/irGwuP3Cpwkw3h7raDcvA/kV7+jLUBq//1kW7itofPh8 6xKJ9yCJ35vCS4LDhEeIbtMQXRe27QQXeFfi2VjFFRQRQeJlKf0nAd8sH/4q1fPa IVKy6lgZcDDzCzy0Ux9us8baUOgTjLIY81AKMXGUmXaUNLpDfF6n6RLRtj8fLcN5 sT37okHyRmK9LNwI1PzU8r2p7i3gweDXx4x9g6nqmi6OV8fm4suSd8VOWb8oF1Yb clQWSvSPE3GcnVaepcixIV1PCF53KsCHaPp63RZbKB3MiPuNTdQQi9Xm1Qfrt0UW uJiEzfGXavkNRYaShycMB6HJGfjIsCZH9wzf1/irCuhiy+kJbvQq9lhojWw/V0N3 TGwY0Sd/eWqtmcx+u449viDIfLZsJDjzXXsYV60JdZtsSsd5+Mud+vfOlv3s/oy0 EPnIzMJar5LGPghPuab8g7rcXim77hFxS7eLp4j9ugWgRPa8TRpiBb/wx2c8a2e0 122ZW8Fc7+9gxHxSCJdM7GIFChdN2IMogCkERAuTyo+dNP01Mv07eMvSA6JkgiN2 ZlSJ8M9UHRS7dplpJGE= =nxc8 -----END PGP SIGNATURE-----
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2016-03-01 19:03 +1100 |
| Message-ID | <mailman.59.1456819439.20602.python-list@python.org> |
| In reply to | #103736 |
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
[toc] | [prev] | [next] | [standalone]
| From | Anthony Papillion <anthony@cajuntechie.org> |
|---|---|
| Date | 2016-03-01 22:29 -0600 |
| Message-ID | <mailman.91.1456892958.20602.python-list@python.org> |
| In reply to | #103736 |
-----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-----
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web