Created
November 22, 2019 11:38
-
-
Save gnuton/6a02225b0cbff6547b78723564aee63c to your computer and use it in GitHub Desktop.
AWS SES send file with attachment in bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
use "text/csv" instead of "text/plain"??
Thanks for quick response i have tried this option result is the same as earlier
@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.
source file content as follows
hello 1st line
hello 2nd line
received attachment
hello 1st line hello 2nd line..
What may be the solution
@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
@gnuton Thanks that helped.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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