-1

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?

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

1 Answer 1

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')
2
  • My e-mail provider does not have an API for sending e-mails. This is why I use Brevo. I also want to avoid Google services.
    – viktak
    Commented Feb 3 at 9:31
  • @viktak all email providers have SMTP APIs... that's the way email moves around the system. No SMTP api, means no email at all. Commented Feb 4 at 9:33

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.