|
|
Browse | Submit A New Snippet | Create A Package
Versions Of This Snippet:
Download a raw-text version of this code by clicking on "Download Version"
Latest Snippet Version: 1.0#!/usr/bin/env python import smtplib import time import StringIO import base64 import os.path SMTP_SERVER = 'smtp.domain.tld' SMTP_PORT = 25 smtp_server = smtplib.SMTP(SMTP_SERVER, port=SMTP_PORT) file = '/path/to/attached_file' filename = os.path.split(file)[1] # Headers from_addr = ('from_name', 'to_email') to_addr = ('to_name', 'to_email') date = smtplib.rfc822.formatdate() subject = 'Test' message_id = time.strftime('%s@localhost', time.localtime()) boundary = time.strftime('NextPart%s', time.localtime()) message = [] message.append('From: %s <%s>' % from_addr) message.append('To: %s <%s>' % to_addr) message.append('Date: %s' % date) message.append('Subject: %s' % subject) message.append('Message-Id: <%s>' % message_id) message.append('MIME-Version: 1.0') message.append('Content-Type: multipart/mixed; Boundary="%s"' % boundary) message.append('') message.append('This is a multi-part message in MIME format.') # Mail message.append('') message.append('') message.append('--%s' % boundary) message.append('Content-Type: text/plain;') message.append(' charset="us-ascii"') message.append('Content-Transfer-Encoding: quoted-printable') message.append('') # Message message.append('Test!') # Attached file message.append('') message.append('') message.append('--%s' % boundary) message.append('Content-Type: text/plain;') message.append(' name="%s"' % filename) message.append('Content-Transfer-Encoding: base64') message.append('Content-Disposition: attachment;') message.append(' filename="%s"' % filename) message.append('') # Encode the file input = open(file, 'r') output = StringIO.StringIO() base64.encode(input, output) message.append(output.getvalue().replace('\n', '\r\n')) input.close() output.close() # End of the mail message.append('') message.append('--%s--' % boundary) message.append('') smtp_server.sendmail(from_addr[1], to_addr[1], '\r\n'.join(message)) smtp_server.quit() Submit a new versionYou can submit a new version of this snippet if you have modified it and you feel it is appropriate to share with others.. |
|||||||||||||||||||