Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
webctrl/Email.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
53 lines (41 sloc)
1.68 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import smtplib | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
def sendEmails (sourceAddress, addresses, alarmDate, alarmLocation, alarmMessage, alarmSource): | |
msg = MIMEMultipart() | |
msg['Subject'] = alarmMessage | |
msg['From'] = sourceAddress | |
msg['To'] = addresses | |
content = alarmMessage + '\n' + \ | |
'Date: ' + str(alarmDate) + '\n' + \ | |
'Location: ' + alarmLocation + '\n' + \ | |
'Source: ' + alarmSource | |
msg.attach(MIMEText(content, 'plain')) | |
s = smtplib.SMTP('mail.fhi-berlin.mpg.de') | |
s.sendmail(sourceAddress, addresses, msg.as_string()) | |
s.quit() | |
def getMailAddresses (currentHour, mailAddresses, timePeriods): | |
periods = timePeriods.split(';') | |
foundAPeriod = False | |
for n, period in enumerate(periods): | |
try: | |
times = period.split('-') | |
if int(times[0]) <= currentHour < int(times[1]): | |
foundAPeriod = True | |
break | |
except: | |
errorMessage = "ERROR: Something is wrong with the given period: " + period + " Please check it." | |
return errorMessage | |
if not foundAPeriod: | |
errorMessage = "ERROR: The current hour, " + str(currentHour) + ", is not contained in any of the time periods " \ | |
"specified in the SETTINGS file." | |
return errorMessage | |
try: | |
addresses = mailAddresses.split(';') | |
except IndexError: | |
errorMessage = "ERROR: There are no e-mails specified for the current time period, " + period + \ | |
"Please check again the SETTINGS file" | |
return errorMessage | |
return addresses[n] |