summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAssaf Gordon <assafgordon@gmail.com>2017-06-08 15:35:40 (GMT)
committerAssaf Gordon <assafgordon@gmail.com>2017-06-08 15:35:40 (GMT)
commit275d95d3991990f72853377abe0970ef2c97ec60 (patch)
tree4ba8ad2d33640e2d86358e4beb47d42685f6a0b2
parent545bd5a8d12fc60b23eed0718aa518ba9fe46a6c (diff)
downloadcode-snippets-275d95d3991990f72853377abe0970ef2c97ec60.zip
code-snippets-275d95d3991990f72853377abe0970ef2c97ec60.tar.gz
code-snippets-275d95d3991990f72853377abe0970ef2c97ec60.tar.bz2
mime-smtp-example.py: new scriptHEADmasterdev/mime
-rw-r--r--metadata.yml2
-rwxr-xr-xmime-smtp-example.py76
2 files changed, 78 insertions, 0 deletions
diff --git a/metadata.yml b/metadata.yml
index b97e18a..5445ede 100644
--- a/metadata.yml
+++ b/metadata.yml
@@ -13,6 +13,8 @@ files:
description: 'automatically duplicate stdout/err, save into separate log files'
argparse-more-help.py:
description: 'implementing --more-help option'
+ mime-smtp-example.py:
+ description: 'build and send multipart mime email'
links:
- name: posix-libc-examples
url: https://git.housegordon.org/gitweb/posix-libc-examples.git
diff --git a/mime-smtp-example.py b/mime-smtp-example.py
new file mode 100755
index 0000000..9eefd64
--- /dev/null
+++ b/mime-smtp-example.py
@@ -0,0 +1,76 @@
+#!/usr/bin/env python3
+
+"""
+Copyright (C) 2017 Assaf Gordon <assafgordon@gmail.com>
+License: Simplified BSD (2-clause)
+
+
+A simple examlpe script of preparing a multipart mime message
+(main text + attachments),
+and sending it using an SMTP server.
+"""
+
+import smtplib, os
+from getpass import getuser
+from socket import gethostname
+from email.mime.multipart import MIMEMultipart
+from email.mime.base import MIMEBase
+from email.mime.text import MIMEText
+from email.utils import COMMASPACE, formatdate
+from email import encoders
+import magic # requires 'python-magic' package
+
+
+send_from= getuser() + "@" + gethostname()
+send_to= ["assafgordon@gmail.com"]
+subject="testing 1 2 3"
+
+msg = MIMEMultipart()
+msg['From'] = send_from
+msg['To'] = COMMASPACE.join(send_to)
+msg['Date'] = formatdate(localtime = True)
+msg['Subject'] = subject
+
+
+# First part, attach as inline content
+f = "/etc/motd"
+text = open(f).read()
+msg.attach( MIMEText(text) )
+
+
+# Other files, store as attachments
+files = [
+ "/usr/share/pixmaps/debian-logo.png",
+ "/bin/sed"
+ ]
+
+for f in files:
+ # Detect mime type
+ mime = magic.from_file(f,mime=True)
+ (main,sub) = mime.split("/",2)
+
+ # Read the file, create mime part as 'attachment'
+ part = MIMEBase(main,sub)
+ part.set_payload( open(f,"rb").read() )
+ encoders.encode_base64(part)
+ part.add_header('Content-Disposition',
+ 'attachment; filename="{0}"'.format(os.path.basename(f)))
+
+ msg.attach(part)
+
+
+## Print the entire mime message.
+## This can be pipe'd into "/usr/sbin/sendmail -t" to be send
+## (assuming sendmail is insalled and properly configured)
+if True:
+ print(msg)
+
+
+## Send the message by SMTP.
+## Can be sent to localhost if exim4/postfix/etc are installed and configured.
+## Otherwise, add host/port to the connect() call (and user/password login, too)
+if False:
+ mailer = smtplib.SMTP()
+ mailer.connect()
+ mailer.sendmail(send_from, send_to, msg.as_string())
+ mailer.quit()