Send email attachment using AWS SES CLI - amazon-web-services

I'm trying to send email attachments using the SES CLI, but every time the mail arrives and I open the attachment I get an error in Adobe:
could not open the file because it is either not a supported file type or because the file has been damaged.
The command I'm using is:
aws ses send-raw-email --raw-message file:///root/AWS/INSPECTOR/message.json
And the contents of that file is:
{
"Data": "From: sender#exmple.com\nTo: recipient#example.com\nSubject: Test email sent using the AWS CLI (contains an attachment)\nMIME-Version: 1.0\nContent-type: Multipart/Mixed; boundary=\"NextPart\"\n\n--NextPart\nContent-Type: text/plain\n\nThis is the message body.\n\n--NextPart\nContent-Type: application/pdf;\nContent-Disposition: attachment; filename=\"report.pdf\";\npath=\"\/tmp\/report.pdf\"\n\n--NextPart--"
}
I've seen the page at http://docs.aws.amazon.com/cli/latest/reference/ses/send-raw-email.html but I can't quite get the syntax correct, so any help would be appreciated....

The attachment should be passed in Base64 encoding with specifying Content-Transfer-Encoding: base64 in the MIME.
Here is the link of previous thread where I answered:
Sending aws cli SES as a file attachmennt

I was able to write some code for a college to solve the same issue for plain/text. I did try this with a PDF type but unfortunately I wasn't able to get that working correctly, the received file seemed to be corrupt. I think for other file types you have to encode it in base64 but not sure on the exact structure to be used with the cli.
echo '{"Data": "From: from#domain.com\nTo: to#domain.com\nSubject:
[Subject]\nMIME-Version: 1.0\nContent-type: Multipart/Mixed;
boundary=\"NextPart\"\n\n--NextPart\nContent-Type:
text/plain\n\n[Body]\n\n--NextPart\nContent-Type:
text/plain;\nContent-Disposition: attachment;
filename=\"test.txt\"\n\n'$(cat ./input.txt)'\n--NextPart--"}' >
message.json & aws ses send-raw-email --region eu-west-1 --raw-message
file://./message.json
Essentially the cat command in the middle writes the text into the message.json so that it can be dynamic. Hope this helps someone.
EDIT
Thanks to #James Dean:
The following is an example with a PDF attachment:
echo '{"Data": "From: from#domain.com\nTo: to#domain.com\nSubject:
[Subject]\nMIME-Version: 1.0\nContent-type: Multipart/Mixed;
boundary=\"NextPart\"\n\n--NextPart\nContent-Type:
text/plain\n\n[Body]\n\n--NextPart\nContent-Type:
application/pdf;\nContent-Disposition:
attachment;\nContent-Transfer-Encoding: base64;
filename=\"test.pdf\"\n\n'$(base64 test.pdf)'\n--NextPart--"}' > message.json & aws ses send-raw-email
--region eu-west-1 --raw-message file://./message.json
Cheers,
Alexei Blue.

The sample you tried to adapt adds plain text and embeds it to the email. You are trying to add a pdf, however you are only adding the header to the mail, but you aren't adding the pdfs content.
You need to embed the pdf base64 encoded as well.
Doing a quick search this answer to the slightly different question "How to embed images in email" might help you with the embedding. Instead of an image you want to embedded a pdf in this case.
If properly prepare your json and it should work with the aws-cli.

Using AWS CLI v2 to send a zip file:
echo '{"Data": "From: test#test.com\nTo: test#test.com\nSubject: Test email sent using the AWS CLI (contains an attachment)\nMIME-Version: 1.0\nContent-type: Multipart/Mixed; boundary="NextPart"\n\n--NextPart\nContent-Type: text/plain\n\nThis is the message body.\n\n--NextPart\nContent-Type: application/zip;\nContent-Disposition: attachment; filename="file.zip"\nContent-Transfer-Encoding: base64\n\n'$(base64 file.zip)'\n\n--NextPart--"}' > message2.json; /usr/local/bin/aws ses send-raw-email --cli-binary-format raw-in-base64-out --raw-message file://message2.json
This way you have encoded the file in base64, the HEADER specifies that, and then you send the rest of the data in raw format, and the CLI will encode that for you.

Related

Decoding an Encrypted Authorization/Error message in AWS

Some actions that involve IAM permissions may return a Client.UnauthorizedOperation responses.
You can decrypt the message from the CLI using the following command:
$> aws sts decode-authorization-message --encoded-message <encoded message from error>
This will give you an output that looks like:
{"allowed":false,"explicitDeny":false,"matchedStatements":{"items":[]},"failures":{"items":[]},"context":{"principal":{"id":"APOZIAANAVSK6I6FK2RQI:i-66c78ee7","arn":"arn:aws:sts::<aws-account-id>:assumed-role/my-role-ec2/i-123456e7"},"action":"iam:PassRole","resource":"arn:aws:iam::<aws-account-id>:role/my-role-ec2","conditions":{"items":[]}}}
The error message is actually encoded JSON inside "", by default the embedded quotes (") are escaped as \"; to facilitate reading the error, extract the message portion and use a text editor to replace \" with ".

Aws lex fulfillment with aws lambda

I have a problem to Play audio message from aws lex code hook .is there any option can return audio file instead of text response on content .guys any ideas please share me.
Amazon LEX does not talk. If you want speaking functionality, look at using Amazon Polly, which is a service that turns text into lifelike speech.
Amazon Lex uses Polly to deliver audio responses.
You'll find the output voice setting under the general settings tab of your Lex bot in the Amazon Lex Console.
Programatically you need to invoke the PostContent method instead of PostText. The PostContent method accepts an audio stream and in turn returns an audio stream.
This page from the Developer Guide describes the main points to consider when sending and receiving voice streams to and from the Lex runtime API.
Amazon Lex Developer Guide | PostContent

Wrap JPEG image in a multipart header using AWS Lambda#Edge

I have been trying to read the AWS Lambda#Edge documentation, but I still cannot figure out if the following option is possible.
Assume I have an object (image.jpg, with size 32922 bytes) and I have setup AWS as static website. So I can retrieve:
$ GET http://example.com/image.jpg
I would like to be able to also expose:
$ GET http://example.com/image
Where the response body would be a multipart/related file (for example). Something like this :
--myboundary
Content-Type: image/jpeg;
Content-Length: 32922
MIME-Version: 1.0
<actual binary jpeg data from 'image.jpg'>
--myboundary
Is this something supported out of the box in the AWS Lambda#Edge API ? or should I use another solution to create such response ? In particular it seems that the response only deal with text or base64 (I would need binary in my case).
I finally was able to find complete documentation. I eventually stumble upon:
API Gateway - PORT multipart/form-data
which refers to:
Enabling binary support using the API Gateway console
The above documentation specify the steps to handle binary data. Pay attention that you need to base64 encode the response from lambda to pass it to API Gateway.

aws ses automation script : Error parsing parameter '--destination': Expected: '=', received: '#' for input:

I've been a reader for a while. Usually deal with stuff on my own but today I need a hint.
So I've been tasked to run an email campaign at my new job (100k audience) with minimal budget spent and very short notice (tomorrow). I leaned toward AWS (Amazon Web Services) because Amazon SES (Simple Email Service) is very cheap and you only pay what you use.
tried to use AWS SDK on command line. I made a tiny script to read the adresses file (.txt) line by line and execute a send mail command
#! /bin/bash
file='./test-list.txt'
from='sender#domain.com'
for line in $(<$file)
do
#send mail
aws ses send-templated-email --source=$from --destination=$line --template 'test' --template-data ""
done
Script returned the error message :
Error parsing parameter '--destination': Expected: '=', received: '#' for input:
To:recipient#domain.com
Now I'm not the bash king, but I've tried different parameter formulations and nothing got me rid of the error.
each line from the input file is a correct email address, and this looks like a parsing issue, but I don't get it.
any ideas ? It would help a ton :)
You need to use the correct format for destination from https://docs.aws.amazon.com/cli/latest/reference/ses/send-templated-email.html#options
Example using send-email:
aws ses send-email --from "example.com" --destination "ToAddresses=${line}" --text "hello world!" --subject "test"
Example like the one given:
aws ses send-templated-email --source $from --destination "ToAddresses=${line}" --template "test" --template-data ""

boto3 publish message SNS

I think i am trying to do something really simple but I am doing something wrong in some point or missing something.
Goal: Send a message from python 2.7 using the boto3 (aws sdk library)
I have the application Key, registrationId, etc...and my code is
response = client.publish(
TargetArn=platform_endpoint['EndpointArn'],
Message="Hi there"
)
Then i receive the messageId from aws, and the notification popup in the terminal movil, but always empty, without any text, "Hi there" in this case. I tried to use the aws SNS console and works, the phone receive the notification with text normally.
I also tried to send a JSON, but same result, and if the easiest goal is failing...better fix this to go for JSON :p
Any suggestion is welcome
Finally the solution was use json.dumps to convert the JSON to string
response = client.publish(
TargetArn=platform_endpoint['EndpointArn'],
Message=json.dumps(jsonObj),
MessageStructure='json'
)