简体   繁体   English

将Sendgrid与Symfony2集成

[英]Integrate Sendgrid with Symfony2

I have a problem trying to integrate sendgrid library in Symfony2. 尝试在Symfony2中集成sendgrid库时遇到问题。 We have copied the library inside our bundle and we have included it in a service. 我们已将库复制到包中,并将其包含在服务中。 If this service is called by an action, the library it is running successfully. 如果某个操作调用了该服务,则说明该库正在成功运行。

This is the service: 这是服务:

use Symfony\Component\DependencyInjection\ContainerInterface;

require_once(__DIR__.'/SendgridPhp/sendgrid-php.php');

class MailerService extends \Twig_Extension {
    protected $container;
    private $mailer;
    private $templating;

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

    public function sendEmail($to, $from, $subject, $body, $attachment = null)
    {   
        $sendgrid = new \SendGrid($this->container->getParameter('sendgrid_user'),  $this->container->getParameter('sendgrid_password'));

        $email = new \SendGrid\Email();

        $email->setFrom($from)
                ->setFromName('Name')
                ->setSubject($subject)
                ->addTo($to)
                ->setHtml($body, 'text/html');

        $salida = $sendgrid->send($email );
    }
}

The problem happens when we call this service from a symfony2 command. 当我们从symfony2命令调用此服务时,就会发生问题。

  $mailerService = $container->get('mailer.service');
  $mailerService->sendEmail($user->getEmail(), $container->getParameter("sender_email"), 'Message', $body);

The error in namespace is the next: 接下来是名称空间中的错误:

PHP Fatal error:  Class 'SendGrid\Email' not found in /var/www/SpainStartup/src/SpainStartup/CommunicationBundle/Services/MailerService.php on line 35

Should we do something special to load the library in command context ? 我们是否应该做一些特殊的事情以在命令上下文中加载库?

Thanks in advance 提前致谢

You'll need to autoload the library. 您需要自动加载库。 The files for third-party code usually go in the vendor folder. 第三方代码的文件通常位于供应商文件夹中。 You can add it to your composer.json in the require section: 您可以在require部分中将其添加到composer.json中:

{
    "require": {
        // Other dependencies...
        "sendgrid/sendgrid": "2.0.5"
    }
}

Remove what you "copied" in. Then run composer update wherever your composer.json file is located. 删除“复制”到的内容。然后在composer.json文件所在的任何位置运行composer update

After this it should 'just work'. 在此之后,它应该“正常工作”。

Instead of using any sendgrid's library, I just worked upon the php example they provided on the api site. 我没有使用任何sendgrid的库,而是研究了它们在api网站上提供的php示例。 This stand-alone php code works flawlessly for me: 这个独立的php代码对我来说完美无缺:

sendmail.php sendmail.php

<?php
function sendgridmail($from, $to, $subject, $message, $headers)
{
    print_r('entering the function');
    $url = 'https://api.sendgrid.com/';
    $user='shinujacob';
    $pass='mypassword';

    $params = array(
        'api_user'  => $user,
        'api_key'   => $pass,
        'to'        => $to,
        'subject'   => $subject,
        'html'      => '',
        'text'      => $message,
        'from'      => $from,
      );

    $request =  $url.'api/mail.send.json';

    // Generate curl request
    $session = curl_init($request);

    // Tell curl to use HTTP POST
    curl_setopt ($session, CURLOPT_POST, true);

    // Tell curl that this is the body of the POST
    curl_setopt ($session, CURLOPT_POSTFIELDS, $params);

    // Tell curl not to return headers, but do return the response
    curl_setopt($session, CURLOPT_HEADER, false);

    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
        //print_r('obtaining the response');
    // obtain response
    $response = curl_exec($session);
    print_r('closing curl session');
    curl_close($session);

    // print everything out

    //print_r($response);

}



//only for testing:
/*$to      = 'shinujacobrocks@yahoo.com';
$subject = 'Testemail';
$message = 'It works!!';
echo 'To is: ' + $to;
#wp_mail( $to, $subject, $message, array() );
sendgridmail($to, $subject, $message, array());
print_r('Just sent!');*/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM