Welcome Guest, Not a member yet? Register   Sign In
Loading Mailjet API
#1

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?
Reply
#2

I believe the first slash on 'use \Mailjet\Resources;' should be 'use Mailjet\Resources', doesn't Mailjet have a Exception handler to check if something has gone wrong with the communication?
Reply
#3

Why don't you show the code for the api call in the controller?
Reply
#4

(This post was last modified: 09-29-2022, 10:42 AM by kcs.)

(09-29-2022, 02:54 AM)superior Wrote: I believe the first slash on 'use \Mailjet\Resources;' should be 'use Mailjet\Resources', doesn't Mailjet have a Exception handler to check if something has gone wrong with the communication?

Thank you so much, that was it Smile
I was not seeing anything return as the example seems to only show the result when it works with this line:
Code:
$response->success() && var_dump($response->getData());

But I was not seeing anything... and since I don't really understand what I am doing for now... I have no idea how to try something different than what I am getting from the documentation explicitly.

(09-29-2022, 04:06 AM)kenjis Wrote: Why don't you show the code for the api call in the controller?
Sorry I thought it was not relevant at this point. Thanks for your help
Reply




Theme © iAndrew 2016 - Forum software by © MyBB