Giter Site home page Giter Site logo

Comments (22)

MkLHX avatar MkLHX commented on August 16, 2024 2

Hi @antoine-nedelec,
This is part of my composer.json

"repositories": [
	  {
		"type": "git",
		"url": "https://github.com/sendinblue/APIv3-php-library.git"
	  }
],
"require": {
        "sendinblue/api-v3-sdk": "*@dev"
}

I've add sendinblue API class repository and require api-v3-sdk

Next i create a Service in symfony src/Services/SendinblueApi.php


namespace App\Services;

use SendinBlue\Client\Configuration;
use Symfony\Component\DependencyInjection\Container;

/**
 * Class SendinblueApi
 *
 * @package App\Services
 */
class SendinblueApi
{
	
	/**
	 * @var Container
	 */
	protected $container;
	
	/**
	 * @var Configuration
	 */
	protected $configuration;
	
	/**
	 * construct
	 *
	 * @param Container     $container
	 * @param Configuration $configuration
	 */
	public function __construct($container, $configuration)
	{
		$this->container = $container;
		$this->configuration = $configuration;
	}
	
	/**
	 *
	 */
	public function setApiKey()
	{
		// Configure API key authorization: api-key
		$this->configuration::getDefaultConfiguration()
			->setApiKey('api-key', $this->container->getParameter("sendinblue_api_key_v3"));
	}
	
}

And in my controller i use it like this :

$this->container->get('sendinblue_api')->setApiKey();
$api_instance = new SMTPApi();
$templateId = 8; //registration template
			
$sendEmail = new SendEmail();
$sendEmail->setEmailTo([$user->getEmail()])
		->setReplyTo('[email protected]')
		->setAttributes(["USERNAME" => $user->getUsername(), "LINK" => $link]);	
$result = $api_instance->sendTemplate($templateId, $sendEmail);

from sendinblue-api-bundle.

MkLHX avatar MkLHX commented on August 16, 2024

Dont find this sorry : https://github.com/sendinblue/APIv3-php-library

from sendinblue-api-bundle.

antoine-nedelec avatar antoine-nedelec commented on August 16, 2024

How did you do it in the end ?

from sendinblue-api-bundle.

MkLHX avatar MkLHX commented on August 16, 2024

Hi @antoine-nedelec see link below

from sendinblue-api-bundle.

antoine-nedelec avatar antoine-nedelec commented on August 16, 2024

Yeah i have "There is no extension able to load the configuration for "sendin_blue_api" (in....". So I wondered how you did it ?

from sendinblue-api-bundle.

MkLHX avatar MkLHX commented on August 16, 2024

So i just add the class in my project with composer. Then i make a wrapper to use it.
Can't show gist at this moment but if you can wait i make it later?

from sendinblue-api-bundle.

antoine-nedelec avatar antoine-nedelec commented on August 16, 2024

Yeah I'd like to if you can :). I did added:
"sendinblue/api-v3-sdk": "^6.0",
in my composer, but when I update it, I have this error when clearing the cache:

Script cache:clear returned with error code 1
!!
!! In FileLoader.php line 168:
!!
!! There is no extension able to load the configuration for "sendin_blue_api"
!! (in /Users/antoinenedelec/Documents/symfony/boxbox/config/packages/sendin_b
!! lue_api.yaml). Looked for namespace "sendin_blue_api", found "framework", "
!! web_server", "sensio_framework_extra", "twig", "web_profiler", "monolog", "
!! debug", "security", "fos_user", "doctrine_cache", "doctrine", "doctrine_mig
!! rations", "maker", "swiftmailer" in /Users/antoinenedelec/Documents/symfony
!! /boxbox/config/packages/sendin_blue_api.yaml (which is loaded in resource "
!! /Users/antoinenedelec/Documents/symfony/boxbox/config/packages/sendin_blue_
!! api.yaml").
!!
!!
!! In YamlFileLoader.php line 657:
!!
!! There is no extension able to load the configuration for "sendin_blue_api"
!! (in /Users/antoinenedelec/Documents/symfony/boxbox/config/packages/sendin_b
!! lue_api.yaml). Looked for namespace "sendin_blue_api", found "framework", "
!! web_server", "sensio_framework_extra", "twig", "web_profiler", "monolog", "
!! debug", "security", "fos_user", "doctrine_cache", "doctrine", "doctrine_mig
!! rations", "maker", "swiftmailer"

Maybe I also installed something that comes in conflict..

from sendinblue-api-bundle.

antoine-nedelec avatar antoine-nedelec commented on August 16, 2024

Ok I added all of those, but the problem still remains in composer I think as I have this message:

Executing script cache:clear [KO]
[KO]
Script cache:clear returned with error code 1
!!
!! In FileLoader.php line 168:
!!
!! There is no extension able to load the configuration for "sendin_blue_api"
!! (in /Users/antoinenedelec/Documents/symfony/boxbox/config/packages/sendin_b
!! lue_api.yaml). Looked for namespace "sendin_blue_api", found "framework", "
!! web_server", "sensio_framework_extra", "twig", "web_profiler", "monolog", "
!! debug", "security", "fos_user", "doctrine_cache", "doctrine", "doctrine_mig
!! rations", "maker", "swiftmailer" in /Users/antoinenedelec/Documents/symfony
!! /boxbox/config/packages/sendin_blue_api.yaml (which is loaded in resource "
!! /Users/antoinenedelec/Documents/symfony/boxbox/config/packages/sendin_blue_
!! api.yaml").
!!
!!
!! In YamlFileLoader.php line 657:
!!
!! There is no extension able to load the configuration for "sendin_blue_api"
!! (in /Users/antoinenedelec/Documents/symfony/boxbox/config/packages/sendin_b
!! lue_api.yaml). Looked for namespace "sendin_blue_api", found "framework", "
!! web_server", "sensio_framework_extra", "twig", "web_profiler", "monolog", "
!! debug", "security", "fos_user", "doctrine_cache", "doctrine", "doctrine_mig
!! rations", "maker", "swiftmailer"
!!
!!
!!
Script @auto-scripts was called via post-update-cmd

Thanks a lot for the details, i'll try to see why I get this error message

from sendinblue-api-bundle.

MkLHX avatar MkLHX commented on August 16, 2024

So i read you haven't got a sendin_blue_api.yaml file on the config/packages folder. Try to add it.....

from sendinblue-api-bundle.

antoine-nedelec avatar antoine-nedelec commented on August 16, 2024

I fixed that problem, my mistake.. But how do you link configuration to your service ? Autowire doesn't work for me, and I don't know what to send ? (I saw the "Configuration.php" in the SendInBlue extension but how do i link it ?)

from sendinblue-api-bundle.

antoine-nedelec avatar antoine-nedelec commented on August 16, 2024

I'm still having issues with "$this->configuration = $configuration;". As i can't find any reference for this in my service.yaml:

App\Services\SendinBlueApi:
    arguments:
        $container: '@service_container'
        $configuration: ''

How did you link that variable ?

from sendinblue-api-bundle.

MkLHX avatar MkLHX commented on August 16, 2024

Hello,
Have you declare your service on config/services.yaml?
like this :

services:
   //....
    sendinblue_api:
        class:  App\Services\SendinblueApi
        arguments: ['@service_container',\SendinBlue\Client\Configuration]
        public: true
   ....//

from sendinblue-api-bundle.

antoine-nedelec avatar antoine-nedelec commented on August 16, 2024

All right thanks, was trying to get the "Configuration" with "@" or "%"..

Actually I read online that for S4 the best practice is to use the class name in the service declaration, and not to use the "class" attribute anymore :). Like:

services:
   //....
    App\Services\SendinblueApi:
        arguments: ['@service_container',\SendinBlue\Client\Configuration]
        public: true
   ....//

Btw I have a 401 response now: [401] Client error: POST https://api.sendinblue.com/v3/smtp/templates/62/send resulted in a 401 Unauthorized response:
{"code":"unauthorized","message":"Key not found"}

But the key is the v3 one right ?

from sendinblue-api-bundle.

antoine-nedelec avatar antoine-nedelec commented on August 16, 2024

Nevermind I found a workaround :). Thanks a lot for your help !!! You can close that issue 👍

from sendinblue-api-bundle.

MkLHX avatar MkLHX commented on August 16, 2024

Nice! Congrats!

I'd like to get the best pratices for all my projects under symfony but they always make changes and it's difficule to be up to date....

from sendinblue-api-bundle.

Noido avatar Noido commented on August 16, 2024

Hi @antoine-nedelec got same problem, how you do for resolve this problem ?

https://api.sendinblue.com/v3/smtp/templates/1/send resulted in a 401 Unauthorized response:
{"code":"unauthorized","message":"Key not found"}

Thanks for help

from sendinblue-api-bundle.

antoine-nedelec avatar antoine-nedelec commented on August 16, 2024

Hi @Noido, nut sure which part doesn't work for you, so here is some code I use:

composer.json

    [...]
    "repositories": [
        {
            "type": "git",
            "url": "https://github.com/sendinblue/APIv3-php-library.git"
        }
    ],
    "require": {
        [...]
        "sendinblue/api-v3-sdk": "^6.0.0",
        [...]
    },
    [...]

services.yaml

parameters:
    [...]
    sendinblue.apikey.v3: 'XXXXXXXXXXXX...'
    [...]

services:
    [...]
    App\Services\SendinBlueApi:
        arguments:
            $container: '@service_container'
            $configuration: \SendinBlue\Client\Configuration
            [...]

Services/SendinBlueApi.php

namespace App\Services;

use SendinBlue\Client\Configuration;
use Symfony\Component\DependencyInjection\Container;

/**
 * Class SendinblueApi
 *
 * @package App\Services
 */
class SendinBlueApi {

    /**
     * @var Container
     */
    protected $container;

    /**
     * @var Configuration
     */
    protected $configuration;

    /**
     * construct
     *
     * @param Container     $container
     * @param Configuration $configuration
     */
    public function __construct($container, $configuration) {
        $this->container = $container;
        $this->configuration = $configuration;
    }
}

Services/MailerService.php

namespace App\Services;

use FOS\UserBundle\Mailer\MailerInterface;
use FOS\UserBundle\Model\UserInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use SendinBlue\Client\Api\SMTPApi;
use SendinBlue\Client\Model\SendEmail;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\Router;

class MailerService implements MailerInterface {

    private $container;
    private $router;

    public function __construct(ContainerInterface $container,  Router $router) {
        $this->container = $container;
        $this->router = $router;
    }

    /**
     * $dest is primary dest (array)
     * $bcc is cc dest (array)
     * $template is sendinblue template ID
     * $attributes is attributes in template that you can replace
     */
    public function sendEmailForTemplate($dest, $bcc, $template, $attributes) {

        $api_instance = new SMTPApi();
        $api_instance->getConfig()->setApiKey('api-key', $this->container->getParameter('sendinblue.apikey.v3'));

        $sendEmail = new SendEmail();
        $sendEmail->setEmailTo($dest);
        if($bcc) { $sendEmail->setEmailBcc([$bcc]); }

        $sendEmail
            ->setReplyTo($this->container->getParameter('mailer_user'))
            ->setAttributes($attributes);

        try {
            return $api_instance->sendTemplate($template, $sendEmail);
        } catch (\Exception $e) {
            error_log($e->getMessage());
        }
    }

    public function sendConfirmationEmailMessage(UserInterface $user) {
        // TODO: Implement your sendConfirmationEmailMessage() method.
    }

    public function sendResettingEmailMessage(UserInterface $user) {
        // TODO: Implement your sendResettingEmailMessage() method.
    }
}

do not forget to set "$this->container->getParameter('mailer_user')" in services.yaml, or if it can be different each time you change my function sendEmailForTemplate so it takes it in parameter.

Enjoy :)
Antoine

from sendinblue-api-bundle.

Noido avatar Noido commented on August 16, 2024

Yes its ok thanks, i just add new service and it s good ;)

from sendinblue-api-bundle.

Noido avatar Noido commented on August 16, 2024

@antoine-nedelec

Got other problem

i want to send register email to a new user register on site with a template, this user is not in my contacts list on sendinblue so i do :

$sendEmail
->setReplyTo("[email protected]")
->setAttributes($attributes);

But i have no idea how create attributes i try $attributes = ["WELCOME" => "test"] but not work mail is empty from attribute welcome

In my template on sendinblue i put {{ WELCOME }} i try to {{ contact.WELCOME }} but not work too

Can you help me again thanks

from sendinblue-api-bundle.

antoine-nedelec avatar antoine-nedelec commented on August 16, 2024

So you can create a new template in sendinblue, the you put something like this or anything you want:

<p style="font-size:12px;">Hi,</p>
<p style="font-size:12px;">
	<strong>Your access to my website:</strong><br />
	- user : %MAIL_LOGIN%<br />
	- password : %MAIL_PASSWORD%</p>
<p style="font-size:12px;">You can use this <a href="%MAIL_LINK%" target="_blank">link</a> to connect.</p>
<p style="font-size:12px;">Regards</p>
<p style="font-size:12px;">...</p>

So you have 3 attributes here in this template:

  • MAIL_LOGIN
  • MAIL_PASSWORD
  • MAIL_LINK

then I would call my function like this:

    $attributes = array(
        "MAIL_LOGIN" => $login,
        "MAIL_PASSWORD" => $pw,
        "MAIL_LINK" => $link);

    $this->mailerService->sendEmailForTemplate($dest, null, 101, $attributes);

you can adapt this depending on how you changed it ;)

from sendinblue-api-bundle.

antoine-nedelec avatar antoine-nedelec commented on August 16, 2024

NB: I recently changed the way I do it, I create a folder named "emails" in "templates" where I have all my emails written in symfony, and only 1 template in sendinblue.

Then i do smthing like:

    $content = $this->container->get('templating')->render($template, [
        'title' => $title,
        'login' => $email,
        'password' => $password
    ]); 

   // In object in sendinblue, put %MAIL_TITLE% and in content put %MAIL_CONTENT%
    $attributes = array(
        "MAIL_TITLE" => $title,
        "MAIL_CONTENT" => $content);

    $this->mailerService->sendEmailForTemplate($email, null, 101, $attributes);

So my emails are rendered in symfony, then I inject the while HTML into the sendinblue template.

Do as you wish, both works fine ;)

from sendinblue-api-bundle.

Noido avatar Noido commented on August 16, 2024

Ok thanks it works with % but doc say bracket they must update i think

from sendinblue-api-bundle.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.