This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author murilobr
Recipients murilobr
Date 2010-07-19.01:55:12
SpamBayes Score 0.0010024101
Marked as misclassified No
Message-id <1279504516.14.0.42498978414.issue9297@psf.upfronthosting.co.za>
In-reply-to
Content
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@domain1.com", "test@domain2.com", "test", host="smtp.domain1.com", port=25, auth=True, user="user", passw="pass")
History
Date User Action Args
2010-07-19 01:55:16murilobrsetrecipients: + murilobr
2010-07-19 01:55:16murilobrsetmessageid: <1279504516.14.0.42498978414.issue9297@psf.upfronthosting.co.za>
2010-07-19 01:55:13murilobrlinkissue9297 messages
2010-07-19 01:55:12murilobrcreate