AWS SES S3 process inbound email - amazon-s3

I'm working on a publish by email system based on AWS SES. For all incoming emails I've set routing to save messages in an S3 bucket so I can asynchronously process them. The problem I have is that the messages are saved in the S3 bucket in a raw format: headers, email body, etc + the encrypted attachment (a huge string) - all in a single file.
Is there a way to break the email message apart form the attachment and save both in separate files at AWS SES level? I'm trying to get the data in the format I need straight from AWS and avoid adding another processing step to the process.
If AWS SES doesn't provide such a feature, what would be the proper way to process these messages to obtain the result described above?

It doesn't look possible to have SES automatically split up the email for you. As per the documentation here:
Amazon SES provides you the raw, unmodified email, which is typically
in Multipurpose Internet Mail Extensions (MIME) format.
I would use S3 or SNS to trigger a Lambda function whenever SES puts a new email file to S3. The Lambda function could split the file however you wish, then write those new files to another S3 bucket.

For anyone coming back later on to this question, this is the link to the JSON structure that you get when you invoke a Lambda function from SES.
http://docs.aws.amazon.com/ses/latest/DeveloperGuide/receiving-email-notifications-examples.html
It took some searching to arrive at that page ;-)
From the link, a Lambda notification would look like this,
{
"notificationType": "Received",
"receipt": {
"timestamp": "2015-09-11T20:32:33.936Z",
"processingTimeMillis": 406,
"recipients": [
"recipient#example.com"
],
"spamVerdict": {
"status": "PASS"
},
"virusVerdict": {
"status": "PASS"
},
"spfVerdict": {
"status": "PASS"
},
"dkimVerdict": {
"status": "PASS"
},
"action": {
"type": "S3",
"topicArn": "arn:aws:sns:us-east-1:012345678912:example-topic",
"bucketName": "my-S3-bucket",
"objectKey": "\email"
}
},
"mail": {
"timestamp": "2015-09-11T20:32:33.936Z",
"source": "0000014fbe1c09cf-7cb9f704-7531-4e53-89a1-5fa9744f5eb6-000000#amazonses.com",
"messageId": "d6iitobk75ur44p8kdnnp7g2n800",
"destination": [
"recipient#example.com"
],
"headersTruncated": false,
"headers": [
{
"name": "Return-Path",
"value": "<0000014fbe1c09cf-7cb9f704-7531-4e53-89a1-5fa9744f5eb6-000000#amazonses.com>"
},
{
"name": "Received",
"value": "from a9-183.smtp-out.amazonses.com (a9-183.smtp-out.amazonses.com [54.240.9.183]) by inbound-smtp.us-east-1.amazonaws.com with SMTP id d6iitobk75ur44p8kdnnp7g2n800 for recipient#example.com; Fri, 11 Sep 2015 20:32:33 +0000 (UTC)"
},
{
"name": "DKIM-Signature",
"value": "v=1; a=rsa-sha256; q=dns/txt; c=relaxed/simple; s=ug7nbtf4gccmlpwj322ax3p6ow6yfsug; d=amazonses.com; t=1442003552; h=From:To:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:Date:Message-ID:Feedback-ID; bh=DWr3IOmYWoXCA9ARqGC/UaODfghffiwFNRIb2Mckyt4=; b=p4ukUDSFqhqiub+zPR0DW1kp7oJZakrzupr6LBe6sUuvqpBkig56UzUwc29rFbJF hlX3Ov7DeYVNoN38stqwsF8ivcajXpQsXRC1cW9z8x875J041rClAjV7EGbLmudVpPX 4hHst1XPyX5wmgdHIhmUuh8oZKpVqGi6bHGzzf7g="
},
{
"name": "From",
"value": "sender#example.com"
},
{
"name": "To",
"value": "recipient#example.com"
},
{
"name": "Subject",
"value": "Example subject"
},
{
"name": "MIME-Version",
"value": "1.0"
},
{
"name": "Content-Type",
"value": "text/plain; charset=UTF-8"
},
{
"name": "Content-Transfer-Encoding",
"value": "7bit"
},
{
"name": "Date",
"value": "Fri, 11 Sep 2015 20:32:32 +0000"
},
{
"name": "Message-ID",
"value": "<61967230-7A45-4A9D-BEC9-87CBCF2211C9#example.com>"
},
{
"name": "X-SES-Outgoing",
"value": "2015.09.11-54.240.9.183"
},
{
"name": "Feedback-ID",
"value": "1.us-east-1.Krv2FKpFdWV+KUYw3Qd6wcpPJ4Sv/pOPpEPSHn2u2o4=:AmazonSES"
}
],
"commonHeaders": {
"returnPath": "0000014fbe1c09cf-7cb9f704-7531-4e53-89a1-5fa9744f5eb6-000000#amazonses.com",
"from": [
"sender#example.com"
],
"date": "Fri, 11 Sep 2015 20:32:32 +0000",
"to": [
"recipient#example.com"
],
"messageId": "<61967230-7A45-4A9D-BEC9-87CBCF2211C9#example.com>",
"subject": "Example subject"
}
}
}

Regarding the question on how to write a Lambda. Here is a portion of our Lambda. The main thing to take out of it is the parseEvent function. and data.event.Records[0] which will give you details
exports.handler = function(event, context, callback) {
var AWS = require('aws-sdk');
// Validate characteristics of a SES event record.
if (!event ||
!event.hasOwnProperty('Records') ||
event.Records.length !== 1 ||
event.Records[0].hasOwnProperty('eventSource') ||
event.Records[0].eventSource !== 'aws:ses' ||
event.Records[0].eventVersion !== '1.0') {
callback(null, {'disposition':'STOP_RULE_SET'});
}
email = data.event.Records[0].ses.mail;
subjectLine = event.Records[0].ses.mail.commonHeaders.subject;
}
The key is the event.Record[0].ses.mail. Unfortunately, I can't find the structure of it via a Google search, I am sure I had seen it before.

Related

Vonage WhatsApp messages suddenly stopped working

I have recently joined a company that is using Vonage and we have WhatsApp communications which was working fine in both dev and production that have both suddenly stopped working.
The shape of the incoming json looks to have changed significantly, but I have changed the code that is reading this and am able to read the messages into the system.
The problem that I have now, is outgoing message is not being accepted.
If I send this json body over
{
"from": "4474183xxxxx",
"to": "4474719xxxxx",
"message": {
"Type": "text",
"content": "Hello! I’m CAI, A Virtual Chat-bot assistant. blurb, more blurb.... .\n\n1. Okay I understand\n\nChoose any one option. Type \"1\" to choose first option."
}
}
I get the following response
{
"Status": "fail", // custom one to my company
"e3": {
"Error": {
"body": {
"type": "https://developer.vonage.com/api-errors",
"title": "Your request parameters didn't validate.",
"detail": "Found errors validating 1 of your submitted parameters.",
"invalid_parameters": [
{
"name": "to",
"reason": "Malformed JSON body."
}
],
"instance": "cf4bce73-2db5-4102-b7af-xxxxxx"
},
"headers": {
"date": "Wed, 02 Nov 2022 14:02:08 GMT",
"content-type": "application/problem+json",
"content-length": "287",
"connection": "close",
"x-envoy-upstream-service-time": "3",
"x-frame-options": "deny",
"x-xss-protection": "1; mode=block",
"strict-transport-security": "max-age=31536000; includeSubdomains",
"x-content-type-options": "nosniff",
"server": "envoy"
},
"statusCode": 400
},
"StatCode": 400,
"Response": null
},
"a": {
"label": 6,
"trys": [
[
0,
6,
null,
7
]
],
"ops": []
}
}
However, looking at the shape of the message that is on the website (https://dashboard.nexmo.com/messages/sandbox) and send this message
{
"from": "xxxx",
"to": "xxxx",
"message_type": "text",
"text": "Hello! I’m CAI, blurb... .\n\n1. Okay I understand\n\nChoose any one option. Type \"1\" to choose first option.",
"channel" : "whatsapp"
}
I get this response
{
"Status": "fail",
"e3": {},
"a": {
"label": 6,
"trys": [
[
0,
6,
null,
7
]
],
"ops": []
},
"Message": "Cannot read property 'Type' of undefined"
}
I would be grateful if someone could help me shape the message that needs to be sent to vonage to that this can be sent out to the end user \ recipient correct.
Thanks
Simon

How to receive the media id from the Whatsapp Business Cloud API?

I have deployed my webhook and connected my WABA. Once I send an image to this business account. It did not return the media id from the response. Actually, the JSON returned to me like this:
{
"entry": [
{
"changes": [
{
"field": "messages",
"value": {
"contacts": [
{
"profile": {
"name": "XXXXXXX"
}
}
],
"messages": [
{
"from": "XXXXXXXXXX",
"id": "wamid.aisjdoiajsodiajsodasd\u003d",
"timestamp": "1657527108",
"type": "image"
}
],
"metadata": {}
}
}
],
"id": "124071984791824"
}
],
"object": "whatsapp_business_account"
}
Or should I try the Whatsapp On-premises API? https://developers.facebook.com/docs/whatsapp/on-premises/reference/media/media-id
You have to chooose the image_id from the request you receive.
like , let media_id=req.body.entry[0].changes[0].value.messages[0].image.id;
you can store this id in DB and use the endpioint where you can get the url for media_id.
Then you can download the image from the URL received and uploaded it anywhere you want.

Is SMS MFA Status in Cognito user pools set by calling setPreferredMFA or is that something else?

when using setPreferredMFA the SMS MFA Status in Cognito user pools is disabled even if setPreferredMFA is set.
What does SMS MFA Status represent and what does it do when I enable it or disable it?
Thank you
This is nothing more but an inconsistency in AWS console/API responses. Example:
Let's enable SMS MFA for a user:
aws cognito-idp set-user-mfa-preference --sms-mfa-settings Enabled=true,PreferredMfa=true --access-token <value>
Yes, in console it still looks as if SMS MFA was not enabled. But this is not true. Let's get our user's data:
aws cognito-idp get-user --access-token <value>
{
"Username": "your-email#example.com",
"UserAttributes": [
{
"Name": "sub",
"Value": "491a3eba-381f-4c87-a7d6-befa21e49e82"
},
{
"Name": "email_verified",
"Value": "true"
},
{
"Name": "phone_number_verified",
"Value": "true"
},
{
"Name": "phone_number",
"Value": "+1234567890"
},
{
"Name": "email",
"Value": "your-email#example.com"
}
],
"PreferredMfaSetting": "SMS_MFA",
"UserMFASettingList": [
"SMS_MFA"
]
}
What you want to look at is the PreferredMfaSetting attribute. It tells you what your user choose for himself/herself.
And if you now try to authenticate like this:
aws cognito-idp initiate-auth --auth-flow USER_PASSWORD_AUTH --client-id <value> --auth-parameters USERNAME=<value>,PASSWORD=<value>
You will receive a response like this:
{
"ChallengeName": "SMS_MFA",
"Session": "<session-value>",
"ChallengeParameters": {
"CODE_DELIVERY_DELIVERY_MEDIUM": "SMS",
"CODE_DELIVERY_DESTINATION": "+*********7890",
"USER_ID_FOR_SRP": "your-email#example.com"
}
}
Ok, so what is this thing in console doing? It is actually deprecated. Take a look at the documentation of the MFAOptions here: https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_GetUser.html
So let's just enable SMS MFA through the console and then check the output of GetUser:
{
"Username": "your-email#example.com",
"UserAttributes": [
{
"Name": "sub",
"Value": "491a3eba-381f-4c87-a7d6-befa21e49e82"
},
{
"Name": "email_verified",
"Value": "true"
},
{
"Name": "phone_number_verified",
"Value": "true"
},
{
"Name": "phone_number",
"Value": "+1234567890"
},
{
"Name": "email",
"Value": "your-email#example.com"
}
],
"MFAOptions": [
{
"DeliveryMedium": "SMS",
"AttributeName": "phone_number"
}
],
"PreferredMfaSetting": "SMS_MFA",
"UserMFASettingList": [
"SMS_MFA"
]
}
That's pretty much it.

Is it reasonable to be concerned an SES object won't be available in S3?

I've setup SES Rule in the following way:
Actions:
1) S3: Saves SES object to an S3 bucket
2) Lambda: Triggers my lambda function for email processing
In my testing, I've always been able to retrieve my SES object from the bucket using the messageID in the very first line of code. I'm then able to parse and read it without issue.
My question is, is it reasonable to be concerned that the SES object may not always be immediately available? I'm considering adding error handling incase the object isn't there. Basically to wait 1/2 a second and try again until the lambda times out. But I don't want to complicate the code if this is not a reasonable concern, handled by boto3, ect. Thoughts?
In your case, it is best to use only one S3 action configured with a notification on a SNS topic and have your Lambda subscribe to this topic.
Your Lambda will receive a SNS event containing a stringified SES event in the message:
{
"Records": [
{
"EventSource": "aws:sns",
"EventVersion": "1.0",
...
"Sns": {
"Type": "Notification",
"MessageId": "...",
"TopicArn": "...",
"Subject": "Amazon SES Email Receipt Notification",
"Message": "<STRINGIFIED SES EVENT>",
...
}
}
]
}
If you parse the Message, you will get something like this:
{
"notificationType": "Received",
"mail": {
"timestamp": "...",
"source": "...",
"messageId": "...",
"destination": [
...
],
"headersTruncated": false,
"headers": [
...
],
"commonHeaders": {
"returnPath": "...",
"from": [
"..."
],
"date": "...",
"to": [
...
],
"messageId": "...",
"subject": "..."
}
},
"receipt": {
...
"action": {
"type": "S3",
"topicArn": "...",
"bucketName": "<YOUR_BUCKET>",
"objectKey": "<YOUR_OBJECT_KEY>"
}
}
}
where you will find the exact reference to the uploaded object in your bucket (receipt.action.bucketName and receipt.action.objectKey).
With this setup, it reasonable to consider that when your Lambda is triggered the object is available.

Detect the id of a sent mail

I would like to detect the message-id of an email
sent from the gmail interface?
what are your proposals?
Thanks.
One way of doing it would be to first list messages:
GET https://www.googleapis.com/gmail/v1/users/me/messages
Response:
{
"messages": [
{
"id": "14f60b097be71757",
"threadId": "14f60b097be71757"
},
{
"id": "14f603ead78aff97",
"threadId": "14f603ead78aff97"
}, ...
Then, get the message you want, and just ask for the Message-Id-header:
format = metadata
metadataHeaders = Message-Id
GET https://www.googleapis.com/gmail/v1/users/me/messages/14f60b097be71757?format=metadata&metadataHeaders=Message-Id
Response:
{
"id": "14f60b097be71757",
"threadId": "14f60b097be71757",
"labelIds": [
"INBOX",
"CATEGORY_PROMOTIONS",
"UNREAD"
],
"snippet": "Cool snippet...",
"historyId": "545168",
"internalDate": "1440436229000",
"payload": {
"mimeType": "multipart/alternative",
"headers": [
{
"name": "Message-Id",
"value": "<10342275.20150824171029.55db500501e9a7#example.com>" // Here it is!
}
]
},
"sizeEstimate": 73995
}