Hi everyone,
I am trying to create a newsletter subscription form using the Mailjet API. I have never used an API before and don't really know what I am doing wrong, but it looks like I am not even able to load the Mailjet class and would really appreciate some help.
My form as just 2 text input, one for email address and a hidden one for the locale I will need later.
I am following the documentation
https://github.com/mailjet/mailjet-apiv3-php.
- I installed the wrapper with composer. I now have a mailjet folder in my vendor folder
- I got the 2 API keys from my Mailjet account, and stored them inside .env as MJ_APIKEY_PUBLIC and MJ_APIKEY_PRIVATE
- I created a form controller and form view with the fields I need, and validations are in place
I am now trying to add Mailjet to the controller, so that when the validations are okay, I can make the API call to add the email to my Mailjet contacts. But I have no idea how to do this as I keep running into errors (or nothing which does not tell me what's happening).
The documentation says to initalise the Mailjet client like this:
Code:
use \Mailjet\Resources;
$apikey = getenv('MJ_APIKEY_PUBLIC');
$apisecret = getenv('MJ_APIKEY_PRIVATE');
$mj = new \Mailjet\Client($apikey, $apisecret);
Here's my controller:
PHP Code:
<?php
namespace App\Controllers;
use \Mailjet\Resources;
class FormController extends BaseController
{
public function index()
{
// load form validation library
helper(['form']);
// the form has been posted
if($this->request->getPost()) {
// we define the rules for the different fields
$rules = [
'email' => 'required|valid_email',
'lang' => 'required|in_list[en,fr]'
];
// rules are validated
if($this->validate($rules)){
$mj = new \Mailjet\Client(getenv('MJ_APIKEY_PUBLIC'), getenv('MJ_APIKEY_PRIVATE'), true, ['version' => 'v3']);
// do the api call and display the subscribed view
return view('subscribed', $this->viewData);
} else {
$this->viewData['validation'] = $this->validator;
return view('form', $this->viewData);
}
}
return view('form', $this->viewData);
}
}
But when I look at the loaded files on the debug console, I don't see anything related to Mailjet ; and already inside my IDE, I can see that the use clause remains grey (if I use \codeigniter it appears white), so I am thinking something is missing at this point, or I am not doing things the way I should.
Can someone help me out?