<?php
namespace WebBundleService;
use SymfonyComponentHttpFoundationJsonResponse;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationResponse;
class Mailjet
{
private $apiKey = "yourKey";
private $mailjetApiSecret = "mailjetApiSecret" ;
private $message ;
private $emails ;
private $sujet ;
private $from ;
private $fromName ;
private $to ;
public function __construct( $message , $sujet , $from , $fromName , $to , $emails )
{
$this->message = $message;
$this->emails = $emails;
$this->sujet = $sujet;
$this->fromName = $fromName;
$this->from = $from;
$this->to = $to;
}
public function send (){
$messageData = array(
'Messages' => array(
array(
'From' => array(
'Email' => $this->from,
'Name' => $this->fromName
),
'To' => array(
array(
'Email' => $this->emails,
'Name' => $this->to
)
),
'Subject' => $this->sujet,
'HTMLPart' => $this->message
)
)
);
$jsonData = json_encode($messageData);
$ch = curl_init('https://api.mailjet.com/v3.1/send');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_USERPWD, "{$this->apiKey}:{$this->mailjetApiSecret}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen($jsonData)
]);
$response = json_decode(curl_exec($ch));
return$response ;
}
}
// how to use in your code
// public function sendEmail(Request $request )
// {
// $test = new Mailjet("Your message","Subject","youremail@host.com",'your name',"you client name", "clientEmail@host.com" );
// $result = $test->send();
// dump( $result);die();
// }