2

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....

freginold
  • 3,946
  • 3
  • 13
  • 28

4 Answers4

3

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

James Dean
  • 4,033
  • 1
  • 9
  • 18
2

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.

Alexei Blue
  • 1,762
  • 3
  • 21
  • 36
0

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.

Udo Held
  • 12,314
  • 11
  • 67
  • 93
0

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.

user2551208
  • 55
  • 1
  • 6