API Email creation

I’m trying to create a new email, but getting an error. This is the curl call:

$curl = curl_init();
echo '.init.';
// Set some options - we are passing in a user agent too here
  curl_setopt_array($curl, array(
  CURLOPT_RETURNTRANSFER => 1,
  CURLOPT_URL => "https://".$loginname.":".$password."@".$siteurl."/api/emails/new",
  CURLOPT_USERAGENT => 'Mautic Email Connector',
  CURLOPT_POST => 1,
// posting the payload
  CURLOPT_POSTFIELDS => array(
    'fromName' => $fromName,
    'name' => $emailName,
    'subject' => iconv_mime_decode($emailSubject,0,"UTF-8"),
    'language' => 'de',
    'template' => 'blank-grapejs',
    'emailType' => 'list',
    'isPublished' => 0,
    'customHtml' => $htmlTemplate,
    'plainText' => '',
    'createdBy' => 1,
    'createdByUser' => 'Auto', 
   'category' => null,
    'useOwnerAsMailer' => 0,
    'lists[]' => 10,
     'publishUp' => date("Y-m-d").' 16:00:00', 
     'publishDown' => date("Y-m-d").' 18:00:00',
    'unsubscribeForm' => null
  )
));
$response = curl_exec($curl);

I’m getting this error all the time:
mautic.CRITICAL: Uncaught PHP Exception Symfony\Component\Debug\Exception\FatalThrowableError: “Type error: Argument 2 passed to Mautic\CoreBundle\Helper\ArrayHelper::getValue() must be of the type array, null given, called in /var/www/html/mautic/plugins/GrapesJsBuilderBundle/Model/GrapesJsBuilderModel.php on line 73” at /var/www/html/mautic/app/bundles/CoreBundle/Helper/ArrayHelper.php line 27 {“exception”:"[object] (Symfony\Component\Debug\Exception\FatalThrowableError(code: 0): Type error: Argument 2 passed to Mautic\CoreBundle\Helper\ArrayHelper::getValue() must be of the type array, null given, called in /var/www/html/mautic/plugins/GrapesJsBuilderBundle/Model/GrapesJsBuilderModel.php on line 73 at /var/www/html/mautic/app/bundles/CoreBundle/Helper/ArrayHelper.php:27)"}

/mautic/plugins/GrapesJsBuilderBundle/Model/GrapesJsBuilderModel.php on line 73
is:
$customHtml = ArrayHelper::getValue('customHtml', $this->requestStack->getCurrentRequest()->get('emailform'));

It’s driving me crazy, where do I make a mistake?

1 Like

Bump!
Anyone?

1 Like

According to the error message there is an argument missing in the provided getValue. The argument must be an array variable and you are giving the script ‘null’.

Could the missing argument be ‘exception’? Not 100% sure how to read these error messages… Something like this:

getValue('customHtml', $exception, $this->requestStack->getCurrentRequest()->get('emailform'));

I have no clue what would be in $exception, but maybe that makes sense for you?

Edit:

This is line 27 in app/bundles/CoreBundle/Helper/ArrayHelper.php:

public static function getValue($key, array $origin, $defaultValue = null)

So the ArrayHelper.php script requires an argument $origin, which should be an array. Again, I have no clue what should be in this variable. Can anyone else fill in?

Hmm what is the $origin? :slight_smile:
The email itself is created, but I get an error :frowning:

I have no clue what array $origin is supposed to be. Here is the full function - or whatever these thingies are called:

/**
 * If the $key exists in the $origin array then it will return its value.
 *
 * @param mixed $key
 * @param mixed $defaultValue
 *
 * @return mixed
 */
public static function getValue($key, array $origin, $defaultValue = null)
{
    return array_key_exists($key, $origin) ? $origin[$key] : $defaultValue;
}

Here’s someone who had the same problem. He solved it using magic :rainbow: