[Tutor] email with body AND attachments

Rodrigues op73418@mail.telepac.pt
Wed Jul 23 16:30:12 2003


> -----Original Message-----
> From: tutor-admin@python.org
> [mailto:tutor-admin@python.org]On Behalf Of
> Paul D. Lusk
> Sent: quarta-feira, 23 de Julho de 2003 18:48
> To: tutor@python.org
> Subject: [Tutor] email with body AND attachments
>
>
> I'm trying to use the email package to create a message with
> both a body and one or more attachments. Using the code in
> the docs (email
> examples - node 12.2.10), I can create messages with all
> the attachments
> that I want, but nothing I've tried gives both attachments
> and a message
> body. Google is coming up blank on examples using the email
> package, though
> I did find examples using pre-email modules. Does anybody
> have an example
> handy using the email package?
>
> Paul
>

This is a class I wrote a while ago to simplify things when it comes
to writing MIME email mesages -- and never got to use it as a matter
of fact.

#Import standard modules.
import smptlib
import email
import mimetypes


#Exceptions.
class SendError(Exception):
    """SendError exception class."""
    pass


class MessageError(Exception):
    """MessageError exception class."""
    pass


#Classes.
class Message(object):
    """The Message class, a simplified MIME wrapper."""

    types = {'text' : email.MIMEText.MIMEText}

    def __init__(self, message, subject, fromaddress, toaddresses):
        """The initializer."""
        super(Message, self).__init__()
        #Generate message.
        MIMEText = email.MIMEText.MIMEText
        self.__message = MIMEText(message, _encoder =
email.Encoders.encode_quopri)
        #Set fields.
        self.__message['Subject'] = subject
        self.__message['From'] = fromaddress
        self.__message['To'] = ';'.join(toaddresses)

    #Properties.
    def __get_sender(self):
        return self.__message['From']

    sender = property(__get_sender)

    def __get_recipients(self):
        return self.__message['To'].split(';')

    recipents = property(__get_recipients)

    def __str__(self):
        return self.__message.as_string()

    def attach(self, filename):
        """Attach a file to Message."""
        #Generate MIME message.
        filetype, encoding = mimetypes.guess_type(filename)
        maintype, subtype = filetype.split('/', 1)
        f = file(filename)
        try:
            data = f.read()
        finally:
            f.close()
        try:
            message = self.types[maintype](data, _subtype = subtype)
        except KeyError:
            raise MessageError("Unknown file type.", filename)
        #Attach message.
        self.__message.add_header('Content-Disposition',
                                  'attachment',
                                  filename = filename)
        self.__message.attach(message)

As you can see from the __init__ method a message with a body *is* a
email.MIMEText.MIMEText instance. You then attach files
in the usual manner.

Hope it helps, with my best regards,
G. Rodrigues

P.S: I have no accompanying unit tests, so you must take this code as
untested. Don't trust it.

>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor