Download
""" send email with image attached Jean-Claude Rimbault (pynokio.org, 2005) """ import os, rfc822, socket, time, random # parameters orig = 'john.doe@hotmail.com' dest = 'webmaster@pynokio.org' server = '195.101.111.1' # SMTP server address for dest subject = 'your image' text = 'here you go...' image = 'c:\\nokia\\images\\photo(171).jpg' mimetype = 'image/jpeg' domain = 'pynokio' # send mail with image attached filename = os.path.basename(image) data = open(image, 'rb').read() now = time.time() date = rfc822.formatdate(now) number = random.randint(0, 0xffffff) msgid = '<%X.%X@%s>' % (now, number, domain) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) print 'connecting to SMTP server...' s.connect((server, 25)) print 'sending headers...' f = s.makefile() assert f.readline().startswith('2') print >>f, 'helo pynokio' f.flush() assert f.readline().startswith('2') print >>f, 'mail from:', orig f.flush() assert f.readline().startswith('2') print >>f, 'rcpt to:', dest f.flush() assert f.readline().startswith('2') print >>f, 'data' f.flush() assert f.readline().startswith('3') print >>f, 'Message-ID:', msgid print >>f, 'Date:', date print >>f, 'From:', orig print >>f, 'MIME-Version: 1.0' print >>f, 'To:', dest print >>f, 'Subject:', subject boundary = '------' + date.encode('hex') print >>f, 'Content-Type: multipart/mixed; boundary="%s"' % boundary print >>f print >>f, 'This is a multi-part message in MIME format.' print >>f, '--' + boundary print >>f, 'Content-Type: text/plain; charset=utf-8' print >>f print >>f, text print >>f, '--' + boundary print >>f, 'Content-Type: %s; name="%s"' % (mimetype, filename) print >>f, 'Content-Transfer-Encoding: base64' print >>f, 'Content-Disposition: attachment; filename="%s"' % filename print >>f print 'sending data...' print >>f, data.encode('base64') print >>f, '--' + boundary + '--' print >>f print >>f, '.' f.flush() assert f.readline().startswith('2') print >>f, 'quit' f.flush() s.close() print '%d bytes image sent in %d s' % (len(data), time.time() - now)