Skip to content

Instantly share code, notes, and snippets.

@gnuton
Created November 22, 2019 11:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gnuton/6a02225b0cbff6547b78723564aee63c to your computer and use it in GitHub Desktop.
Save gnuton/6a02225b0cbff6547b78723564aee63c to your computer and use it in GitHub Desktop.
AWS SES send file with attachment in bash
function sendCSVviaEmail {
echo "Sending file via email..."
AWS_CLI="/usr/bin/aws"
SENDER=${EMAIL_SENDER}
RECIPIENT=${EMAIL_RECIPIENT}
SUBJECT=${EMAIL_SUBJECT}
BODY=${EMAIL_BODY}
ATTACHMENT_TYPE="text/plain"
ATTACHMENT_FILE_NAME="report.csv"
ATTACHMENT_FILE_TO_READ_FROM_DISK="*.csv"
# Create message
echo '{"Data": "From:'${SENDER}'\nTo:'${RECIPIENT}'\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:'${ATTACHMENT_TYPE}';\nContent-Disposition: attachment; filename=\"'${ATTACHMENT_FILE_NAME}'\"\n\n'$(cat ./${ATTACHMENT_FILE_TO_READ_FROM_DISK})'\n--NextPart--"}' > message.json
if [ -f "$AWS_CLI" ]; then
$AWS_CLI ses send-raw-email --region eu-central-1 --raw-message file://message.json
else
echo "WARNING: Skipping sending email. AWS CLI not found in path. This should be okay if the script doesn't run in the docker image."
fi
}
@ganeshpore
Copy link

ganeshpore commented Jan 17, 2022

Hi ,
tried to use above script , i am able to receive attached csv file as well however its not receiving in correct format, Can you please suggest any input for the same
actual data
Name , status
user1 ,active

however in attachment file is receiving like below example after aws ses ( New line char is missing in attached file after receiving from aws ses)
name,status user1,active

@gnuton
Copy link
Author

gnuton commented Jan 18, 2022

use "text/csv" instead of "text/plain"??

@ganeshpore
Copy link

ganeshpore commented Jan 18, 2022

Thanks for quick response i have tried this option result is the same as earlier

@Shreenivasayg
Copy link

Shreenivasayg commented Dec 1, 2022

@gnuton . I am using above template. I am facing an issue where the content of the file is printed continuously, meaning if the 2 statements are in 2 lines in original file in the disk . But in the mail attachment that i have received displays in single line. That makes receiver to read content confusing.

@Shreenivasayg
Copy link

source file content as follows
hello 1st line
hello 2nd line

received attachment
hello 1st line hello 2nd line..

What may be the solution

@gnuton
Copy link
Author

gnuton commented Dec 1, 2022

@Shreenivasayg
AWS SES API has body AND html check this
If you use send html instead of txt that should work.
aws/aws-cli#3561

@Shreenivasayg
Copy link

@gnuton Thanks that helped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment