-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* change var name and add support of setting python env var via terraform * add smtp support for email notifications * add smtp vars in terraform * add plain mail body, custom key delete period per user and var name change * update smtp logging and other bug fixes * update var description
- Loading branch information
1 parent
2eb5d94
commit 15babf3
Showing
10 changed files
with
232 additions
and
54 deletions.
There are no files selected for viewing
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import boto3 | ||
import os | ||
import smtplib | ||
import ssl | ||
import logging | ||
|
||
from email.mime.text import MIMEText | ||
from email.mime.multipart import MIMEMultipart | ||
|
||
ssm = boto3.client('ssm', region_name=os.environ.get('AWS_REGION')) | ||
|
||
logger = logging.getLogger('smtp-mailer') | ||
logger.setLevel(logging.INFO) | ||
|
||
# Whether to use SSL or TLS for sending mail | ||
SMTP_PROTOCOL = os.environ.get('SMTP_PROTOCOL', 'ssl') | ||
|
||
# Port number to use for connecting to SMTP server | ||
SMTP_PORT = os.environ.get('SMTP_PORT', 465) | ||
|
||
# Host name of server to connect | ||
SMTP_SERVER = os.environ.get('SMTP_SERVER', None) | ||
|
||
# SSM Parameter name which holds SMTP server password | ||
SMTP_PASSWORD_PARAMETER = os.environ.get('SMTP_PASSWORD_PARAMETER') | ||
|
||
def send_via_ssl(userName, mailFrom, smtpPassword, mailTo, mailBody): | ||
try: | ||
logger.info('Sending mail to {} ({}) via SMTP over SSL'.format(userName, mailTo)) | ||
context = ssl.create_default_context() | ||
with smtplib.SMTP_SSL(SMTP_SERVER, SMTP_PORT, context=context) as server: | ||
server.login(mailFrom, smtpPassword) | ||
server.sendmail(mailFrom, mailTo, mailBody) | ||
logger.info('Mail sent to {} ({}) via SMTP over SSL'.format(userName, mailTo)) | ||
except Exception as e: | ||
logger.error('Unable to send mail via SSL to {}. Reason: {}'.format(mailTo, e)) | ||
|
||
def send_via_tls(userName, mailFrom, smtpPassword, mailTo, mailBody): | ||
try: | ||
logger.info('Sending mail to {} ({}) via SMTP over TLS'.format(userName, mailTo)) | ||
context = ssl.create_default_context() | ||
with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server: | ||
server.starttls(context=context) | ||
server.login(mailFrom, smtpPassword) | ||
server.sendmail(mailFrom, mailTo, mailBody) | ||
logger.info('Mail sent to {} ({}) via SMTP over TLS'.format(userName, mailTo)) | ||
except Exception as e: | ||
logger.error('Unable to send mail via TLS to {}. Reason: {}'.format(mailTo, e)) | ||
|
||
def send_email(mailTo, userName, mailSubject, mailFrom, mailBodyPlain, mailBodyHtml): | ||
if SMTP_SERVER is None: | ||
logger.error('SMTP_SERVER value cannot be blank') | ||
return False | ||
|
||
logger.info('Fetching SMTP password from SSM') | ||
resp = ssm.get_parameter( | ||
Name=SMTP_PASSWORD_PARAMETER, | ||
WithDecryption=True | ||
) | ||
smtpPassword = resp['Parameter']['Value'] | ||
|
||
message = MIMEMultipart("alternative") | ||
message["Subject"] = mailSubject | ||
message["From"] = mailFrom | ||
message["To"] = mailTo | ||
|
||
mailBodyPlain = MIMEText(mailBodyPlain, 'plain') | ||
mailBodyHtml = MIMEText(mailBodyHtml, 'html') | ||
|
||
message.attach(mailBodyPlain) | ||
message.attach(mailBodyHtml) | ||
|
||
if SMTP_PROTOCOL.lower() == 'ssl': | ||
send_via_ssl(userName, mailFrom, smtpPassword, mailTo, message.as_string()) | ||
elif SMTP_PROTOCOL.lower() == 'tls': | ||
send_via_tls(userName, mailFrom, smtpPassword, mailTo, message.as_string()) | ||
else: | ||
logger.error('{} is not a supported SMTP protocol'.format(SMTP_PROTOCOL)) |
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
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
Oops, something went wrong.