Support of an HTML Email with AWS SES Alerter #1743
Unanswered
OshriBaruch
asked this question in
Q&A
Replies: 2 comments
-
|
Basic principles for HTML email support HTML emails are sent only if ses_html_body is specified. If not specified, text emails are sent as usual. Implementation image Compatible images alert:
- ses
ses_from_addr: alert@example.com
ses_email:
- ops@example.com
ses_subject: "🔥 ElastAlert detected error"
ses_html_body: |
<html>
<body>
<h2 style="color:red;">ElastAlert</h2>
<p>The following events were detected:</p>
<pre>{{ matches | tojson(indent=2) }}</pre>
</body>
</html>import boto3
import logging
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from elastalert.alerters import Alerter
from elastalert.util import EAException
logger = logging.getLogger(__name__)
class SESAlerter(Alerter):
"""Sends alerts via Amazon SES"""
required_options = frozenset(['ses_email', 'ses_from_addr'])
def __init__(self, *args):
super(SESAlerter, self).__init__(*args)
self.to = self.rule.get('ses_email')
if not isinstance(self.to, list):
self.to = [self.to]
self.cc = self.rule.get('ses_cc', [])
self.bcc = self.rule.get('ses_bcc', [])
self.from_addr = self.rule.get('ses_from_addr')
self.region = self.rule.get('ses_aws_region', 'us-east-1')
self.client = boto3.client(
'ses',
region_name=self.region,
aws_access_key_id=self.rule.get('ses_aws_access_key'),
aws_secret_access_key=self.rule.get('ses_aws_secret_key')
)
def alert(self, matches):
subject = self.create_subject(matches)
body = self.create_alert_body(matches)
# HTML body
html_body = self.rule.get('ses_html_body')
if html_body:
html_body = self.create_custom_body(html_body, matches)
self.send_email(subject, body, html_body)
def send_email(self, subject, body, html_body=None):
"""
Send email via SES.
If html_body is provided, send HTML email using send_raw_email.
Otherwise, send plain text email (backward compatible).
"""
# ---------- HTML ----------
if html_body:
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = self.from_addr
msg['To'] = ', '.join(self.to)
if self.cc:
msg['Cc'] = ', '.join(self.cc)
# text/plain fallback
msg.attach(MIMEText(body, 'plain', 'utf-8'))
# text/html
msg.attach(MIMEText(html_body, 'html', 'utf-8'))
destinations = self.to + self.cc + self.bcc
try:
self.client.send_raw_email(
Source=self.from_addr,
Destinations=destinations,
RawMessage={'Data': msg.as_bytes()}
)
except Exception as e:
raise EAException("Error sending HTML email via SES: %s" % e)
return
# ---------- text ----------
kwargs = {
'Source': self.from_addr,
'Destination': {
'ToAddresses': self.to,
'CcAddresses': self.cc,
'BccAddresses': self.bcc
},
'Message': {
'Subject': {'Data': subject},
'Body': {
'Text': {'Data': body}
}
}
}
try:
self.client.send_email(**kwargs)
except Exception as e:
raise EAException("Error sending email via SES: %s" % e)
def get_info(self):
return {
"type": "ses",
"recipients": self.to,
"cc": self.cc,
"bcc": self.bcc
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
Thank you! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Is it possible to send an HTML Email with AWS SES Alerter?
Beta Was this translation helpful? Give feedback.
All reactions