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


Groups > comp.lang.python > #103736

Re: Sending an email with a binary attachment

From Peter Pearson <pkpearson@nowhere.invalid>
Newsgroups comp.lang.python
Subject Re: Sending an email with a binary attachment
Date 2016-02-29 17:08 +0000
Message-ID <djjc8gFl1hoU1@mid.individual.net> (permalink)
References <mailman.0.1456733421.20602.python-list@python.org>

Show all headers | View raw


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.

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