Web www.gerd-tentler.de
Version 2.2 (released Jan. 14, 2014) [Download]

Usage

Import the mimemail module and create a new instance of the MimeMail class:
import mimemail
mail = mimemail.MimeMail("HTML")
Then set senderName, senderMail, subject, and optionally cc, bcc, replyTo, and priority:
mail.senderName = "sender name"
mail.senderMail = "sender@email"
mail.subject = "This is the subject line"
The mail body either contains your message (plain text or HTML):
mail.body = "Hello! This is a message for you."
Or, if you want to send an already existing HTML file instead, just put its path into the mail body:
mail.body = "path/to/file"
The HTML text will be parsed for images, scripts and stylesheets, and they will be included into your e-mail automatically.

You can also attach files to your e-mail:
mail.attachments.append("path/to/file1")
mail.attachments.append("path/to/file2")
mail.attachments.append("path/to/file3")
...
When all settings are done, create the MIME mail:
mail.create()
And finally send it to the recipient(s):
# recipient list can be a list/tuple...
recipients = ['recipient1@email', 'recipient2@email', 'Recipient3 <recipient3@email>']
if not mail.send(recipients): print mail.error

# ...or a string with comma-separated values
recipients = 'recipient4@email, recipient5@email, Recipient6 <recipient6@email>'
if not mail.send(recipients): print mail.error

Notes

If you include attachments and Python can not locate the files e.g. because of a wrong path, they won't be sent. Same goes of course for images and stylesheets inside your mail body. So before starting a mailing you should first send the e-mail to an address of yours, or save the e-mail using the saveDir option, and check if everything works like it should.

Maybe you ask yourself why you have to call create() explicitely before sending your e-mail with send(). Well, imagine this: You want to send the same e-mail to several recipients, but you also want to personalize it, e.g. by starting with "Dear Peter", "Dear Paul", etc. If you had to create the entire e-mail with all attachments etc. for each recipient again only because the name has changed, this would be a big waste of time and resources. Instead, you create it only once with a variable [NAME] and then replace [NAME] with each recipient's name before sending. Example:
...
mail.subject = "Hello [NAME]!"
mail.body = "Dear [NAME], this is a personal message for you."

# create the MIME mail once
mail.create()

recipients = {'Peter': 'peter@somewhere.com',
              'Paul' : 'paul@somewhere.com',
              'Mary' : 'mary@somewhere.com'}

# for each recipient...
for name, address in recipients.items():

    # replace variables in subject line and body text
    mail.subject = mail.subject.replace('[NAME]', name)
    mail.body = mail.body.replace('[NAME]', name)

    # send e-mail
    if not mail.send('%s <%s>' % (name, address)): print mail.error
This works because the MIMEMAIL class saves the original subject line and body text when the MIME mail is created and restores it each time after sending. Please note that this variable replacement only works with the subject line and the body text, not with any other contents.

Comments