1

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' => 'test@gmail.com',
                        'Name' => 'Test',
                    ],
                    'To' => [
                        'Email' => 'recievetest@gmail.com',
                        ],
                    '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" => "test@gmail.com"
        "Name" => "Test"
      ]
      "To" => array:1 [▼
        "Email" => "tets@gmail.com"
      ]
      "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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Pravin
  • 11
  • 3
  • @MarcinOrlowski I even changed it into plain text. Still recieved the same error. – Pravin Aug 29 '23 at 11:30
  • does example from the documentation work for you? – Marcin Orlowski Aug 29 '23 at 11:31
  • I think your `[ 'json' => $data, ]` you are passing to the `post` method, should be _just_ `$data` ...? (Or `json_encode($data)`, not sure if the library is supposed to handle the encoding itself, when you use that syntax.) – CBroe Aug 29 '23 at 12:13
  • @MarcinOrlowski Yes . thats the problem. it worked now shared the correct code below. Thank you for your help! – Pravin Aug 29 '23 at 12:18

1 Answers1

0

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' => 'test@gmail.com',
                'Name' => 'Test',
            ],
            'To' => [
                [
                    'Email' => 'test@gmail.com',
                ],
            ],
            '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);
}
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Pravin
  • 11
  • 3