Skip to content

Instantly share code, notes, and snippets.

@MkLHX
Last active February 15, 2019 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MkLHX/92d2193f5af13baf35b3665155e94e57 to your computer and use it in GitHub Desktop.
Save MkLHX/92d2193f5af13baf35b3665155e94e57 to your computer and use it in GitHub Desktop.
Send email in SF4 with sendinblue api V3 - https://developers.sendinblue.com/docs
$emailData = [
'tags' => ['registration'],
'templateId' => 8,//use template you make on sendinblue interface
'replyTo' => ['email' => 'contact@example.com'],
'sender' => ['email' => 'noreply@example.com'],
'to' => [['email' => $user->getEmail()]],
'params' => ['USERNAME' => $user->getUsername(), "LINK" => $link],//use variable on sendinblue template
];
$result = $this->container->get('App\Services\SendinblueApi')->apiCall('smtp/email', $emailData);
<?php
namespace App\Services;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Class SendinblueApi
*
* @package App\Services
*/
class SendinblueApi
{
/**
* @var ContainerInterface
*/
protected $container;
/**
* SendindlueApi constructor.
*
* @param ContainerInterface $container
*/
public function __construct($container)
{
$this->container = $container;
}
/**
* @param $uri
* @param array $data
*
* @return mixed
*/
function apiCall($uri, $data)
{
$curl = curl_init();
// Error waiting a die() order
if (empty($curl)) {
die("ERROR curl_init : cURL is not ready");
}
$apiKey = $this->container->getParameter('sendinblue_api_key_v3');
$httpHeader = ['api-key: ' . $apiKey, 'Content-Type: application/json'];
if (in_array(@$_SERVER['REMOTE_ADDR'], ['127.0.0.1', '::1'])) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
}
curl_setopt($curl, CURLOPT_URL, $this->container->getParameter('sendinblue_api_url') . $uri);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_ENCODING, '');
curl_setopt($curl, CURLOPT_MAXREDIRS, 10);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_HTTPHEADER, $httpHeader);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
$result = curl_exec($curl);
// Error management
if (curl_errno($curl)) {
// Show error message
echo "ERROR curl_exec : " . curl_error($curl);
}
curl_close($curl);
return $result;
}
}
parameters:
sendinblue_api_url: 'https://api.sendinblue.com/v3/' # sendinblue api entrypoint
sendinblue_api_key_v3: 'sendinblueapikey' # sendinblue api
App\Services\SendinblueApi:
arguments: ['@service_container']
public: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment