
| Current Path : /home/cgabriel/20_dev/11_iftlib/misc/ |
Linux ift1.ift-informatik.de 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 |
| Current File : //home/cgabriel/20_dev/11_iftlib/misc/mail.py |
# -*- coding: utf-8 -*-
"""
This function converts the content of a markdown file to html
and takes the result as an email content.
Prerequisites
-------------
pandoc has to be installed, then call:
pip install pypandoc
To embed images:
In the markdown file, enter cid: before the path of the image, like
<TD></TD>
Function calls
--------------
python -m iftlib.mail md_mail [parameters]
Default parameters:
md = 'direktmail.md' # markdown content file
Subject = 'IfT'
From = 'christian.gabriel@ift-informatik.de'
To = ''
Cc = ''
Bcc = ''
SMTP = 'localhost' # SMTP server
Parameters:
--md=<markdown content file>
--sub=<subject string>
--from=<sender>
--to=<recipient 1>[,<recipient 2>,...,<recipient n>] | <file containing recipients>
--cc=<recipient 1>[,<recipient 2>,...,<recipient n>] | <file containing recipients>
--bcc=<recipient 1>[,<recipient 2>,...,<recipient n>] | <file containing recipients>
--smtp=<SMTP server>[,<user>,<password>]
--att=<attachment file 1>[,<attachment file 2>,...,<attachment file n>]
"""
import os
import sys
import re
try:
import smtplib
import pypandoc
import email
except:
import pip
# pip.main(["install","smtplib"])
pip.main(["install","pypandoc"])
pip.main(["install","email"])
import smtplib
import pypandoc
import email
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
class Mail (object):
def __init__ (self):
pass
def md_mail(self,pars):
try:
def recipient(type, name):
del msg[type]
if os.path.isfile(name):
with open(name) as f:
adr = f.readlines()
msg[type] = ''.join(adr).replace('\n','')
else:
msg[type] = name
msg = MIMEMultipart()
msg['Subject'] = 'IfT'
msg['From'] = 'christian.gabriel@ift-informatik.de'
msg['To'] = ''
msg['Cc'] = ''
msg['Bcc'] = ''
mdFile = 'direktmail.md'
SMTP = 'localhost'
user = ''
pwd = ''
for p in pars:
p = p.decode(sys.getfilesystemencoding())
m = re.search(r'^--(.*)=(.*)$',p)
if not m:
continue
print m.group(1), m.group(2)
if m.group(1) == "sub":
msg['Subject'] = m.group(2)
if m.group(1) == "from":
msg['From'] = m.group(2)
if m.group(1) == "to":
recipient('To', m.group(2))
if m.group(1) == "cc":
recipient('Cc', m.group(2))
if m.group(1) == "bcc":
recipient('Bcc', m.group(2))
if m.group(1) == "att":
att = m.group(2).split(',')
for a in att:
fp = open(a,'rb')
msgAtt = MIMEApplication(fp.read())
msg.attach(msgAtt)
msgAtt.add_header('Content-Disposition', 'attachment', filename = a)
fp.close()
if m.group(1) == "md":
mdFile = m.group(2)
if m.group(1) == "smtp":
SMTP = m.group(2).split(',')[0]
if len(m.group(2).split(',')) > 1:
user = m.group(2).split(',')[1]
if len(m.group(2).split(',')) > 2:
pwd = m.group(2).split(',')[2]
content = pypandoc.convert(mdFile, 'html')
msg.attach(MIMEText(content, 'html', _charset='utf-8'))
for m in re.finditer('src="cid:.*?"', content):
fp = open(content[m.start()+9:m.end()-1], 'rb')
msgImage = MIMEImage(fp.read())
msg.attach(msgImage)
msgImage.add_header('Content-ID', '<' + content[m.start()+9:m.end()-1] + '>')
fp.close()
sender = smtplib.SMTP(SMTP)
if user != '' and pwd != '':
sender.login(user, pwd)
print (msg['From'], msg['To'].split(',') + msg['Cc'].split(',') + msg['Bcc'].split(','),msg.as_string())
sender.sendmail(msg['From'], msg['To'].split(',') + msg['Cc'].split(',') + msg['Bcc'].split(','),msg.as_string())
sender.quit()
except Exception as inst:
print '\nmd_mail: ' + str(inst)
if __name__ == "__main__":
Mail.__dict__[sys.argv[1]](Mail(),sys.argv[2:])