0

At the beginning, i was using the classic apache sendmail to send email from my custom forms. Finaly i decide to use Mailjet o send all: forms submission and transactional email. I install the plugin and it works well.

As Mailjet API is install throught the plugin, i would like to rely on the plugin to send newsletter subscriptions and unsubscriptions, to send automation trigger.

I check the doc: https://dev.mailjet.com/guides/#contactslist_managemanycontacts And notice than i can use:

$apikey = get_option('mailjet_password');

$apisecret = get_option('mailjet_username');

I try tu use ajax and a function similar to the API documentation but it doesn't seems to work:

function sendRegisterNewsletter($list_id, $email, $firstname, $name, $job, $structure) {

$apikey = get_option('mailjet_password');
$apisecret =  get_option('mailjet_username');

require '../../plugins/mailjet-for-wordpress/api/mailjet-api-v3.php';
use \Mailjet\Resources;
$mj = new \Mailjet\Client($apikey, $apisecret);

}

I get a error with "use": syntax error, unexpected T_USE use

As i use the plugin, i don't think it was useful to use the module: https://github.com/mailjet/mailjet-apiv3-php

Any advice to use the configured plugin and Mailjet's api? Thank you in advance for any help.

jb

0

Because the template is load before the plugin, i finaly decide to add the API directly from https://github.com/mailjet/mailjet-apiv3-php-no-composer

I check the api: https://dev.mailjet.com/guides/?php#contactslist_managemanycontacts

And build my function to send a contact to a list:

// Get the value from the plugin mailjet-for-wordpress
$apisecret = get_option('mailjet_password');
$apikey = get_option('mailjet_username');

require 'mailjet-apiv3-php-no-composer/vendor/autoload.php';

use \Mailjet\Resources;

function sendRegisterNewsletterToMailjet($params) {

$mj = new \Mailjet\Client($apikey, $apisecret);

$body = [
'Action' => "addforce",
'Contacts' => [
[
'Email' => $params['Email'],
'Properties' => [
'prénom' => $params['prénom'],
'nom' => $params['nom'],
'profession' => $params['profession'],
'structure' => $params['structure'],
]
],
]
];

$response = $mj->post(Resources::$ContactslistManagemanycontacts, ['id' => $params['ListID'], 'body' => $body]);
$response->success() && var_dump($response->getData());

wp_send_json_success($response);

}

The problem is i get this response:

{
    "success":true,
    "data":
        {
            "request":{
            "type":"application/json"
        }
    }
}

Instead of:

{
    "Count": 1,
    "Data": [
        {
            "JobID": 35800
        }
    ],
    "Total": 1
}

Any idea?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.