Yes I have mailjet account and I'm using my real API keys in my code.
I tried to Initialize my Mailjet Client like you said, but now I'm getting different error, Failed to send email.
Error:
At least FromEmail or Sender must be provided array(0) { }
But althrough i can see my from email in array when i echo it, I still get this error.
<?php
/*
This call sends a message to one recipient.
*/
require 'vendor/autoload.php';
use \Mailjet\Resources;
$apikey = 'MY_APIKEY';
$apisecret = 'MY_APISECRETKEY';
$mj = new \Mailjet\Client($apikey, $apisecret);
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$body = [
'Messages' => [
[
'From' => [
'Email' => $_POST["email"],
'Name' => "Mailjet Pilot"
],
'To' => [
[
'Email' => "MY_EMAIL",
'Name' => "passenger 1"
]
],
'Subject' => $_POST["subject"],
'TextPart' => $_POST["msg"],
'HTMLPart' => $_POST["msg"]
]
]
];
$response = $mj->post(Resources::$Email, ['body' => $body]);
echo print_r($body);
if ($response->success()) {
$data = $response->getData();
echo "Email was sent successfully.\n";
// Zobraz nebo zpracuj data podle potřeby
var_dump($data);
} else {
echo "Failed to send email. Error: " . $response->getReasonPhrase() . "\n";
// Zobraz nebo zpracuj chybové informace
var_dump($response->getData());
}
}
?>