Merge branch 'master' of ssh://sapsailing.com/home/trac/git

This commit is contained in:
Generic Wiki User
2020-04-17 15:50:02 +00:00
3 changed files with 124 additions and 58 deletions
-38
View File
@@ -1,38 +0,0 @@
// Taken from https://github.com/sirceljm/AWS-SES-simple-mail-forward/blob/master/mail_forward.js
// Install as an AWS Lambda according to https://github.com/sirceljm/AWS-SES-simple-mail-forward
// and configure the to_address environment variable as a JSON array of strings representing
// the recipients' e-mail addresses. Subscribe the lambda to your SNS topic that receives the
// notification messages about your inbound e-mail. Use mail_forward_policy.json for the IAM role's
// security policy.
var AWS = require("aws-sdk");
var ses = new AWS.SES();
exports.handler = (event, context, callback) => {
let data = JSON.parse(event.Records[0].Sns.Message);
let source = data.content;
source = source.replace(/^DKIM-Signature/im, "X-Original-DKIM-Signature");
// source = source.replace(/^From/im, "X-Original-From");
// source = source.replace(/^Source/im, "X-Original-Source");
// source = source.replace(/^Sender/im, "X-Original-Sender");
// source = source.replace(/^Return-Path/im, "X-Original-Return-Path");
source = source.replace(/^Domainkey-Signature/im, "X-Original-Domainkey-Signature");
// source = `From: AWS SES Mail Forwarding <${data.mail.destination}>\n` + source;
if (!process.env.to_address) {
console.log("You need to supply to_address enviromnent variable");
} else if (data.mail.destination == process.env.to_address) {
console.log("MAIL SEND ABORTED! - Forwarded mail address and to_address are the same - THIS CAN CAUSE AN INFINITE LOOP!");
} else {
ses.sendRawEmail({
RawMessage: {
Data: source,
},
Destinations: JSON.parse(process.env.to_address),
}, function (err, data) {
if (err) {
console.error(err, err.stack);
}
});
}
};
+98
View File
@@ -0,0 +1,98 @@
# Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# This file is licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License. A copy of the
# License is located at
#
# http://aws.amazon.com/apache2.0/
#
# This file 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.
import os
import boto3
import email
import re
import json
from botocore.exceptions import ClientError
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
region = os.environ['Region']
def get_message_from_s3(bucket_name, object_key):
# Create a new S3 client.
client_s3 = boto3.client("s3")
# Get the email object from the S3 bucket.
object_s3 = client_s3.get_object(Bucket=bucket_name, Key=object_key)
# Read the content of the message.
file = object_s3['Body'].read()
return file
def create_message(file):
sender = os.environ['MailSender']
recipientList = json.loads(os.environ["MailRecipientsJSON"])
mailobject = email.message_from_string(file.decode('utf-8'))
# Uncomment the following to print all available headers, if needed:
# print(mailobject.keys())
# Set all X- headers
mailobject['X-From'] = mailobject['From']
if not mailobject['Reply-To']:
mailobject['Reply-To'] = mailobject['From']
mailobject['X-To'] = mailobject['To']
mailobject['X-Return-Path'] = mailobject['Return-Path']
# Remove original headers and set to SES Value
del mailobject['From']
mailobject['From'] = sender
del mailobject['To']
mailobject['To'] = ','.join(recipientList)
del mailobject['Return-Path']
mailobject['Return-Path'] = sender
message = {
"Source": sender,
"Destinations": recipientList,
"Data" : mailobject.as_string()
}
return message
def send_email(message):
aws_region = os.environ['Region']
# Create a new SES client.
client_ses = boto3.client('ses', region)
# Send the email.
try:
#Provide the contents of the email.
response = client_ses.send_raw_email(
Source=message['Source'],
Destinations=
message['Destinations']
,
RawMessage={
'Data':message['Data']
}
)
# Display an error if something goes wrong.
except ClientError as e:
output = e.response['Error']['Message']
else:
output = "Email sent! Message ID: " + response['MessageId']
return output
def lambda_handler(event, context):
# Get the unique ID of the message. This corresponds to the name of the file
# in S3.
sns = event['Records'][0]['Sns']
message = json.loads(sns['Message'])
bucket_name = message['receipt']['action']['bucketName']
object_key = message['receipt']['action']['objectKey']
print(f"Received message with object key {object_key} in bucket {bucket_name}")
# Retrieve the file from the S3 bucket.
file = get_message_from_s3(bucket_name, object_key)
# Create the message.
message = create_message(file)
# Send the email and print the result.
result = send_email(message)
print(result)
+26 -20
View File
@@ -1,22 +1,28 @@
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
},
{
"Effect": "Allow",
"Action": [
"ses:SendEmail",
"ses:SendRawEmail"
],
"Resource": "*"
}
]
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"logs:CreateLogStream",
"logs:CreateLogGroup",
"logs:PutLogEvents"
],
"Resource": "*"
},
{
"Sid": "VisualEditor1",
"Effect": "Allow",
"Action": [
"s3:GetObject",
"ses:SendRawEmail"
],
"Resource": [
"arn:aws:s3:::support-emails-sapsailing-com/*",
"arn:aws:ses:eu-west-1:017363970217:identity/*"
]
}
]
}