|
#!/usr/bin/env python |
|
# |
|
# Copyright AlertAvert.com (c) 2016. All rights reserved. |
|
# |
|
# Licensed under the Apache License, Version 2.0 (the "License"); |
|
# you may not use this file except in compliance with the License. |
|
# You may obtain a copy of the License at |
|
# |
|
# http://www.apache.org/licenses/LICENSE-2.0 |
|
# |
|
# Unless required by applicable law or agreed to in writing, software |
|
# distributed under the License is distributed on an "AS IS" BASIS, |
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
# See the License for the specific language governing permissions and |
|
# limitations under the License. |
|
|
|
# Created by M. Massenzio ([email protected]), 2016-05-14 |
|
|
|
import argparse |
|
import logging |
|
import socket |
|
|
|
# See: https://github.com/kootenpv/yagmail |
|
import yagmail |
|
|
|
|
|
FORMAT = '%(asctime)-15s [%(levelname)s] %(message)s' |
|
DATE_FMT = '%m/%d/%Y %H:%M:%S' |
|
|
|
|
|
def parse_args(): |
|
""" Parse command line arguments and returns a configuration object |
|
""" |
|
parser = argparse.ArgumentParser() |
|
parser.add_argument('-f', '--api-key-file', required=True, |
|
help="The file containing the API key") |
|
parser.add_argument('-m', dest='msg', required=True, help="The body of the message") |
|
parser.add_argument('-d', dest='dest', help="The recipient of the email") |
|
parser.add_argument('-s', dest='subject', help='The subject for the email', |
|
default='Alert from {hostname}'.format(hostname=socket.gethostname())) |
|
parser.add_argument('-v', dest='verbose', action='store_true', help='Enables debug logging') |
|
return parser.parse_args() |
|
|
|
|
|
def post_mail(to, subj, msg, **kwargs): |
|
""" Sends the ``msg`` via email to the specified recipient. |
|
|
|
@param to: the recipient for the message |
|
@type to: str |
|
|
|
@param subj: the subject for the email |
|
@type subj: str |
|
|
|
@param msg: the body of the message, in plain text |
|
@type msg: str |
|
|
|
@see: https://github.com/kootenpv/yagmail |
|
""" |
|
contents = """<h3>Automated Alert</h3> |
|
<p>{msg}</p> |
|
<h6>Sent via yagmail, using alert.py © 2016 M. Massenzio, all rights reserved.<br> |
|
See: https://github.com/kootenpv/yagmail</h6> |
|
""".format(msg=msg) |
|
with yagmail.SMTP(kwargs.get('username'), kwargs.get('api_key')) as yag: |
|
yag.send(to, subj, contents) |
|
logging.info('Message "{subj}" sent to {to}'.format(to=to, subj=subj)) |
|
|
|
|
|
def read_api_key(filename): |
|
with open(filename, 'r') as key_file: |
|
username = key_file.readline().rstrip('\n') |
|
api_key = key_file.readline().rstrip('\n') |
|
return {'username': username, 'api_key': api_key} |
|
|
|
|
|
def main(): |
|
config = parse_args() |
|
loglevel = logging.DEBUG if config.verbose else logging.INFO |
|
logging.basicConfig(format=FORMAT, datefmt=DATE_FMT, level=loglevel) |
|
logging.info('Mailer Auto Script starting...') |
|
try: |
|
kwargs = read_api_key(config.api_key_file) |
|
except Exception as ex: |
|
logging.error("Could not read the API Key ({}), aborting", ex) |
|
return |
|
logging.debug('API key retrieved') |
|
post_mail(to=config.dest, msg=config.msg, subj=config.subject, **kwargs) |
|
|
|
|
|
if __name__ == "__main__": |
|
main() |