shell - How to make a clickable link in a JSON file from script - Stack Overflow

admin2025-04-17  3

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?

Share Improve this question asked Jan 30 at 17:11 viktakviktak 11 bronze badge 12
  • Try this: 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:32
  • This question is similar to: how can I build bash hyperlink strings from json fields?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – F. Hauri - Give Up GitHub Commented Jan 30 at 22:32
  • If you don't have any tool for JSON-escaping your string then use email_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:36
  • I'm afraid none of the above proposed solutions passes the "curl test".. Fails with Input must be a valid JSON object – viktak Commented Jan 31 at 12:11
  • 1 Forget about the terminal escape sequences and creta a HTML link instead – Fravadona Commented Feb 1 at 15:04
 |  Show 7 more comments

1 Answer 1

Reset to default 0

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')
转载请注明原文地址:http://anycun.com/QandA/1744903068a89249.html