I'm creating an automation where I want to send a link in an e-mail. The e-mail is sent using a curl-able API.
Here is smallest part of the HTML I want to send and reproduces the problem:
printf -v email_body "Click here: \e]8;;file://1.2.3.4/backup/backup.txt\e\Backup.txt\e]8;;\e\\"
When I display it in the terminal, it works perfectly: screenshot of result
When I try to send it, it fails as it says Input must be a valid JSON object
:
curl --request POST \
--url \
--header 'accept: application/json' \
--header 'api-key: xxxxxxxxx' \
--header 'content-type: application/json' \
--data "${email_header}${email_body}${email_footer}"
Everything else is working fine - email_header and email_footer make it a proper JSON/HTML string - i.e. if I remove email_body or put a simple string in it, it works perfectly.
When I save the email_body
I can see with notepad++ that there are escape characters (which is expected):
screenshot of special characters
I tried to escape the special characters like this:
printf -v email_body "Click here: \\e]8;;file://1.2.3.4/backup/backup.txt\\e\\Backup.txt\\e]8;;\\e\\\\"
Still not proper JSON.
What am I missing?
I'm creating an automation where I want to send a link in an e-mail. The e-mail is sent using a curl-able API.
Here is smallest part of the HTML I want to send and reproduces the problem:
printf -v email_body "Click here: \e]8;;file://1.2.3.4/backup/backup.txt\e\Backup.txt\e]8;;\e\\"
When I display it in the terminal, it works perfectly: screenshot of result
When I try to send it, it fails as it says Input must be a valid JSON object
:
curl --request POST \
--url https://api.brevo.com/v3/smtp/email \
--header 'accept: application/json' \
--header 'api-key: xxxxxxxxx' \
--header 'content-type: application/json' \
--data "${email_header}${email_body}${email_footer}"
Everything else is working fine - email_header and email_footer make it a proper JSON/HTML string - i.e. if I remove email_body or put a simple string in it, it works perfectly.
When I save the email_body
I can see with notepad++ that there are escape characters (which is expected):
screenshot of special characters
I tried to escape the special characters like this:
printf -v email_body "Click here: \\e]8;;file://1.2.3.4/backup/backup.txt\\e\\Backup.txt\\e]8;;\\e\\\\"
Still not proper JSON.
What am I missing?
Seems odd to me to be trying to send emails via shell script.... not only that, feels odd to do it via a rest API.... after all, SMTP mail servers have perfectly good APIs for sending emails more directly. Here's an example of sending an email via gmail direct to an smtp server.
curl --url 'smtps://smtp.gmail.com:465' --ssl-reqd \
--mail-from '[email protected]' \
--mail-rcpt '[email protected]' \
--user '[email protected]:YourPassword' \
-T <(echo -e 'From: [email protected]\nTo: [email protected]\nSubject: Curl Test\n\nHello')
printf 'Text before, \e]8;;%s\e\\%s\e]8;;\e\\\, text after.\n' file://$PWD/myfile.txt "My file here."
, see stackoverflow.com/a/72297683/1765658 – F. Hauri - Give Up GitHub Commented Jan 30 at 22:32email_body='"Click here: \u001b]8;;file://1.2.3.4/backup/backup.txt\u001b\\Backup.txt\u001b]8;;\u001b\\"'
– Fravadona Commented Jan 30 at 22:36Input must be a valid JSON object
– viktak Commented Jan 31 at 12:11