ticketfrei/sendmail.py

32 lines
803 B
Python
Raw Normal View History

2018-01-01 10:23:50 +00:00
#!/usr/bin/env python3
2018-03-28 18:24:21 +00:00
from config import config
2018-01-01 10:23:50 +00:00
from email.mime.text import MIMEText
2018-01-05 10:20:07 +00:00
from email.mime.multipart import MIMEMultipart
2018-03-28 22:31:21 +00:00
import logging
from getpass import getuser
import smtplib
from socket import getfqdn
2018-03-28 22:31:21 +00:00
2018-03-29 00:40:22 +00:00
logger = logging.getLogger(__name__)
2018-01-01 10:23:50 +00:00
2018-03-29 00:40:22 +00:00
def sendmail(to, subject, city=None, body=''):
msg = MIMEMultipart()
if city:
msg['From'] = 'Ticketfrei <%s@%s>' % (city, getfqdn())
else:
msg['From'] = 'Ticketfrei <%s@%s>' % (getuser(), getfqdn())
msg['To'] = to
2018-03-29 00:40:22 +00:00
msg['Subject'] = '[Ticketfrei] %s' % (subject, )
msg.attach(MIMEText(body))
with smtplib.SMTP('localhost') as smtp:
smtp.send_message(msg)
2018-01-01 10:23:50 +00:00
# For testing:
if __name__ == '__main__':
sendmail(config['web']['contact'], "Test Mail",
body="This is a test mail.")