[New-bugs-announce] [issue9297] SMTP with Sqlite3 file attachment
Murilo da Silva
report at bugs.python.org
Mon Jul 19 03:55:16 CEST 2010
New submission from Murilo da Silva <murilobr at gmail.com>:
I'm sending e-mail using SMTP. This e-mail is sent with an Sqlite3 database attached.
When I send the e-mail in the same domain the attachment arrives OK, but if I send it to a different domain it comes crashed!!
The file is added with some base64 code...I don't have any ideia why!
The code mail.py:
import smtplib
import os
import mimetypes
import base64
from email import encoders
from email.message import Message
from email.mime.base import MIMEBase
from email.mime.nonmultipart import MIMENonMultipart
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.mime.audio import MIMEAudio
from email.mime.image import MIMEImage
from email.mime.message import MIMEMessage
from email.mime.text import MIMEText
class SendMail:
def __init__(self):
self.body = []
self.attachments = []
def add_body(self, _text, _subtype='plain', _charset='us-ascii'):
part = MIMEText(_text+"\n", _subtype, _charset)
self.body.append(part)
def add_attach(self, _file_path, _subtype='octet-stream', _encoder=encoders.encode_base64, **_params):
path = _file_path
ctype, encoding = mimetypes.guess_type(path)
if ctype is None or encoding is not None:
ctype = 'application/octet-stream'
maintype, subtype = ctype.split('/', 1)
if maintype == 'text':
fp = open(path)
part = MIMEText(fp.read(), _subtype=subtype)
fp.close()
elif maintype == 'image':
fp = open(path, 'rb')
part = MIMEImage(fp.read(), _subtype=subtype)
fp.close()
elif maintype == 'audio':
fp = open(path, 'rb')
part = MIMEAudio(fp.read(), _subtype=subtype)
fp.close()
else:
fp = open(path, 'rb')
part = MIMEBase(maintype, subtype)
part.set_payload(fp.read())
fp.close()
# Encode the payload using Base64
encoders.encode_base64(part)
part.set_payload(part.get_payload().decode())
part.add_header('Content-Disposition', 'attachment', filename = path.split(os.sep)[-1])
self.attachments.append(part)
def send_now(self, mail_from, mail_to, subject, multipart_subtype='mixed', host=None, port=25, auth=False, user="", passw=""):
msg = MIMEMultipart(multipart_subtype)
msg['Subject'] = subject
msg['From'] = mail_from
if type(mail_to) is list:
msg['To'] = ", ".join(mail_to)
else:
msg['To'] = mail_to
for b in self.body:
msg.attach(b)
for att in self.attachments:
msg.attach(att)
try:
mailserver = smtplib.SMTP(host, port)
#mailserver.set_debuglevel(1)
mailserver.ehlo()
mailserver.starttls()
mailserver.ehlo()
if auth:
mailserver.login(user, passw)
mailserver.sendmail(mail_from, mail_to, msg.as_string())
mailserver.close()
return True
except Exception as e:
print(e)
return False
The database to attach is in the final.
The example:
s = SendMail()
s.add_body("test")
s.add_attach(os.path.abspath("test.db"))
s.send_now("test at domain1.com", "test at domain2.com", "test", host="smtp.domain1.com", port=25, auth=True, user="user", passw="pass")
----------
components: Library (Lib)
files: test.db
messages: 110707
nosy: murilobr
priority: normal
severity: normal
status: open
title: SMTP with Sqlite3 file attachment
type: behavior
versions: Python 3.1
Added file: http://bugs.python.org/file18056/test.db
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue9297>
_______________________________________
More information about the New-bugs-announce
mailing list