0

The following codes work and the email was sent out using my sendinblue API:

<?php

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api.sendinblue.com/v3/smtp/email');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{  \n   \"sender\":{  \n      \"name\":\"My name\",\n      \"email\":\"[email protected]\"\n   },\n   \"to\":[  \n      {  \n         \"email\":\"[email protected]\",\n         \"name\":\"His name\"\n      }\n   ],\n   \"subject\":\"Testing email on request\",\n   \"htmlContent\":\"<html><head></head><body><p>Hello,</p>This is my first transactional email sent from Sendinblue.</p></body></html>\"\n}");

$headers = array();
$headers[] = 'Accept: application/json';
$headers[] = 'Api-Key: I removed API Key before posting this question';
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

?>

But I would like to replace the sender name and email with variables but it didn't work out when I replace the

curl_setopt($ch, CURLOPT_POSTFIELDS, "{  \n   \"sender\":{  \n      \"name\":\"My name\",\n      \"email\":\"[email protected]\"\n   },\n   \"to\":[  \n      {  \n         \"email\":\"[email protected]\",\n         \"name\":\"His name\"\n      }\n   ],\n   \"subject\":\"Testing email on request\",\n   \"htmlContent\":\"<html><head></head><body><p>Hello,</p>This is my first transactional email sent from Sendinblue.</p></body></html>\"\n}");

with:

$myname = "My Name";
$myemail = "[email protected]";
curl_setopt($ch, CURLOPT_POSTFIELDS, "{  \n   \"sender\":{  \n      \"name\":$myname,\n      \"email\":$myemail\n   },\n   \"to\":[  \n      {  \n         \"email\":\"[email protected]\",\n         \"name\":\"His name\"\n      }\n   ],\n   \"subject\":\"Testing email on request\",\n   \"htmlContent\":\"<html><head></head><body><p>Hello,</p>This is my first transactional email sent from Sendinblue.</p></body></html>\"\n}");

Can anyone let me know where is the issue?

Sorry, I'm a beginner in coding... Thank you in advanced!

1
  • Remember to always post the message error you are facing so that it will be easier to help and understand the problem. Commented May 1, 2022 at 14:20

1 Answer 1

1

You could be facing an error because you are trying to insert a variable inside a JSON string but missed to wrap the variable with double quotes. Strings in JSON must always be wrapped in double quotes.

Dealing with JSON strings directly like this is generally not a good idea, unless it's a very short or simple string.

A better approach could be to use an array and then convert the array to a string with json_encode, in case you need a JSON string.

Instead of creating an array from scratch, let's be lazy: you can use json_decode to convert the same string used in curl_setopt into an associative array.

$curlPostfields = json_decode("{  \n   \"sender\":{  \n      \"name\":\"My name\",\n      \"email\":\"[email protected]\"\n   },\n   \"to\":[  \n      {  \n         \"email\":\"[email protected]\",\n         \"name\":\"His name\"\n      }\n   ],\n   \"subject\":\"Testing email on request\",\n   \"htmlContent\":\"<html><head></head><body><p>Hello,</p>This is my first transactional email sent from Sendinblue.</p></body></html>\"\n}", 
                              $association = true);

Looking at the docs for json_decode you can see that the option $association = true creates an associative array instead of an object.

The array content is dumped below.

>>> var_dump($curlPostfields);
array(4) {
  ["sender"]=>
  array(2) {
    ["name"]=>
    string(7) "My name"
    ["email"]=>
    string(17) "[email protected]"
  }
  ["to"]=>
  array(1) {
    [0]=>
    array(2) {
      ["email"]=>
      string(21) "[email protected]"
      ["name"]=>
      string(8) "His name"
    }
  }
  ["subject"]=>
  string(24) "Testing email on request"
  ["htmlContent"]=>
  string(114) "<html><head></head><body><p>Hello,</p>This is my first transactional email sent from Sendinblue.</p></body></html>"
}

It's now easy to change as you need the sender name and email fields, you just need to edit directly the array.

$myCustomName = 'Mr. Custom Name';
$myCustomMail = '[email protected]"';

$curlPostfields['sender']['name'] = $myCustomName;
$curlPostfields['sender']['name'] = $myCustomMail;

Now you can just convert back the array to a JSON string, json_encode will handle all the double quotes and stuff.

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($curlPostfields));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.