D
D
Dmaw2020-09-02 18:07:28
PHP
Dmaw, 2020-09-02 18:07:28

What is the correct way to send html to json via curl?

Good evening!
I send mail via unione, I ran into a problem sending mail in html format.
Here is part of my code:

$json = '
{
  "api_key": "'.$api_key.'",
  "message":
  {
    "global_substitutions":
    {
      "someVar":"body.html"
    },
    "body":
    {
      "html": "'.$message.'"
    },
    "subject": "'.$subject.'",
    "from_email": "'.$from.'",
    "from_name": "NAME",
    "reply_to": "'.$reply.'",
    "track_links" : 1,
    "track_read"  : 1,
    "recipients": [
      {
        "email": "'.$to.'"
      }
    ]
  }
}
';

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  curl_setopt($ch, CURLOPT_URL, $url);	
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
  	curl_setopt($ch, CURLOPT_POSTFIELDS, $json);                                                              
  $result = curl_exec($ch);

If we put simple html into the $message variable (highlight it in bold), then the letter is sent, and when I insert html with attributes here, I get an error. Tried escaping quotes, didn't help. Seems like a simple problem :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2020-09-02
@Dmaw

$json = json_encode([
  "api_key" => $api_key,
  "message" => [
    "global_substitutions" => [
      "someVar" => "body.html"
    ],
    "body" => [
      "html" => $message
    ],
    "subject" => $subject,
    "from_email" => $from,
    "from_name" => "NAME",
    "reply_to" => $reply,
    "track_links" => 1,
    "track_read" => 1,
    "recipients": [
      ["email" => $to]
    ]
  ]
]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question