Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send Emails Using Secure SMTP Server #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions opencanary_correlator/common/emailer.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import smtplib
import smtplib, ssl
import mandrill
import opencanary_correlator.common.config as c
from email.mime.text import MIMEText
from opencanary_correlator.common.logs import logger

def send_email(from_='[email protected]', to='', subject='', message='', server='', port=25):
#
# February 6, 2021 - Modifications to the send_email function:
#
# - Add functionality to log in to a secure SMTP server for sending e-mail
#
# NOTE: Customizations are based on https://realpython.com/python-send-email/
# and https://xo.tc/installing-opencanary-on-a-raspberry-pi.html
#
def send_email(from_='[email protected]', to='', subject='', message='', server='', port=25, username='', password=''):
logger.debug('Emailing %s' % to)

if not server:
return

Expand All @@ -15,14 +24,20 @@ def send_email(from_='[email protected]', to='', subject='', message=
msg['From'] = from_
msg['To'] = to

s = smtplib.SMTP(server, port)
try:
s = smtplib.SMTP(server, port)
s.ehlo()
s.starttls()
s.ehlo()
# Log in if appropriate
if username and password:
s.login(username, password)
s.sendmail(from_, [to], msg.as_string())
logger.info('Email sent to %s' % (to))
except Exception as e:
logger.error('Email sending produced exception %r' % e)
s.quit()

finally:
s.quit()

def mandrill_send(to=None, subject=None, message=None, reply_to=None):
try:
Expand All @@ -43,4 +58,5 @@ def mandrill_send(to=None, subject=None, message=None, reply_to=None):
result = mandrill_client.messages.send(message=message, async=False, ip_pool='Main Pool')

except mandrill.Error, e:
print 'Oliver: mandrill_send'
print 'A mandrill error occurred: %s - %s' % (e.__class__, e)
23 changes: 19 additions & 4 deletions opencanary_correlator/common/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,25 @@ def send(self, destination, message):

client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)


client.messages.create(
to=destination,
from_=from_,
body=message
)

#
# February 6, 2021 Modifications to the notify function:
#
# - Rename console.email_notification_address to console.email_notification_addresses
# to reflect the out-of-the-box functionality for handling multiple e-mail addresses
# - Add console.email_from_address
# - Add console.email_credentials parameters to facilitate sending e-mails directly
# using a secure SMTP server, such as Gmail
#
def notify(incident):
if c.config.getVal('console.email_notification_enable', False):
logger.debug('Email notifications enabled')
addresses = c.config.getVal('console.email_notification_address', default=[])
addresses = c.config.getVal('console.email_notification_addresses', default=[])
if c.config.getVal('console.mandrill_key', False):
for address in addresses:
logger.debug('Email sent to %s' % address)
Expand All @@ -32,13 +40,20 @@ def notify(incident):
else:
server = c.config.getVal('console.email_host', default='')
port = int(c.config.getVal('console.email_port', default=25))
from_ = c.config.getVal('console.email_from_address', default='')
credentials = c.config.getVal('console.email_credentials', default=[])
username = credentials[0]
password = credentials[1]
if len(addresses) > 0 and server:
for address in addresses:
send_email(to=address,
send_email(from_=from_,
to=address,
subject=incident.format_title(),
message=incident.format_report(),
server=server,
port=port)
port=port,
username=username,
password=password)

if c.config.getVal('console.sms_notification_enable', default=False):
logger.debug('SMS notifications enabled')
Expand Down