Search code examples
phpmailjetbulk-mail

Missing Mandatory Parameter while sending the API request to Mailjet


I am trying to integrate Mailjet API to send Bulk mails, where I read the documentation and wrote the necessary code to send the request, but I get the error as "ErrorCode" => "mj-0003". The mandatory parameters are From , To , TextPart. Even though I added all these parameters and they are not null either, I get the error as Mandatory parameters missing.

Below is my request code:

    // Prepare Mailjet API request data
        $client = Http::withHeaders([
            'Content-Type' => 'application/json',
        ])->withBasicAuth('d550ed4*************42a9f48ae6', '3a4d*************b2fb');
    
        $data = [
            'Messages' => [
                [
                    'From' => [
                        'Email' => '[email protected]',
                        'Name' => 'Test',
                    ],
                    'To' => [
                        'Email' => '[email protected]',
                        ],
                    'Subject' => $subject,
                    'TextPart' => $content,
                    'HTMLPart' => "<h3>Dear passenger 1, welcome to </h3>",
                    'SandboxMode' => true, 
                ],
            ],
        ];
        // Send email using Mailjet API
        $response = $client->post('https://api.mailjet.com/v3.1/send', [
            'json' => $data,
        ]);
    
        // Process the API response
        $statusCode = $response->status();
        $responseData = $response->json();
    
        if ($statusCode == 200 && $responseData['Messages'][0]['Status'] == 'success') {
            // Success
            // return redirect()->back()->with('success', 'Emails sent successfully.');
            dd($responseData);
        } else {
            // Error
            // return redirect()->back()->with('error', 'Failed to send emails.');
             dd($responseData);
        }

And Below is the $data

array:1 [▼ // app/Http/Controllers/AdminController/ManageBulkEmailController.php:128
  "Messages" => array:1 [▼
    0 => array:6 [▼
      "From" => array:2 [▼
        "Email" => "[email protected]"
        "Name" => "Test"
      ]
      "To" => array:1 [▼
        "Email" => "[email protected]"
      ]
      "Subject" => "Test"
      "TextPart" => "Test Content"
      "HTMLPart" => "<h3>test </h3>"
      "SandboxMode" => true
    ]
  ]
]

And below is the response data $responseData

array:5 [▼ // app/Http/Controllers/AdminController/ManageBulkEmailController.php:128
  "ErrorIdentifier" => "79519bb2-ee10-4759-aaf2-63f5718652da"
  "ErrorCode" => "mj-0003"
  "StatusCode" => 400
  "ErrorMessage" => "Missing mandatory property."
  "ErrorRelatedTo" => array:1 [▼
    0 => "Messages"
  ]
]

I am creating this on Laravel framework. Don't know what I am missing. Here is the link for the documentation.


Solution

  • Okay. it seems like the laravel http client and JSON post method is the problem , i tried it using curl method and now it works.Sharing the correct code below.

    $apiKeyPublic = 'd550*************8ae6';
    $apiKeyPrivate = '3a4db2******************cb2fb';
    
    $postData = [
        'Messages' => [
            [
                'From' => [
                    'Email' => '[email protected]',
                    'Name' => 'Test',
                ],
                'To' => [
                    [
                        'Email' => '[email protected]',
                    ],
                ],
                'Subject' => $subject,
                'TextPart' => $content,
            ],
        ],
         'SandboxMode' => true,
    ];
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://api.mailjet.com/v3.1/send');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json',
    ]);
    curl_setopt($ch, CURLOPT_USERPWD, "$apiKeyPublic:$apiKeyPrivate");
    $response = curl_exec($ch);
    curl_close($ch);
    
    $responseData = json_decode($response, true);
    
    if ($responseData && isset($responseData['Messages'][0]['Status']) && $responseData['Messages'][0]['Status'] == 'success') {
        // Success
        dd($responseData);
    } else {
        // Error
        dd($responseData);
    }