Sending an email with a binary attachment

Peter Pearson pkpearson at nowhere.invalid
Mon Feb 29 12:08:32 EST 2016


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 at cajuntechie.org'
message['reply-to'] = 'xxx at cajuntechie.org'
message['to'] = 'Me <xxx at 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 at 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.



More information about the Python-list mailing list