【问题标题】:Integrate Sendgrid with Symfony2将 Sendgrid 与 Symfony2 集成
【发布时间】:2014-06-06 10:07:18
【问题描述】:

我在尝试将 sendgrid 库集成到 Symfony2 时遇到问题。我们已经将库复制到我们的包中,并将其包含在服务中。如果此服务被某个操作调用,则说明它运行的库成功。

这是服务:

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 );
    }
}

当我们从 symfony2 命令调用此服务时,就会出现问题。

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

命名空间中的错误是下一个:

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

我们应该做一些特殊的事情来在命令上下文中加载库吗?

提前致谢

【问题讨论】:

  • “我们已经复制了包中的库” - 听起来很可疑。通过您的作曲家配置提供它作为依赖项,然后自动加载应该可用。 (这很常见,在您的 Bundle = 只有您的代码,即您编写的代码)。

标签: php symfony sendgrid


【解决方案1】:

您需要自动加载库。第三方代码的文件通常位于供应商文件夹中。您可以在 require 部分将其添加到您的 composer.json 中:

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

删除您“复制”的内容。然后在您的 composer.json 文件所在的任何位置运行 composer update

在此之后它应该“正常工作”。

【讨论】:

  • Composer 更新命令对我不起作用。我在这里想念什么?我使用mac,我通过终端运行命令。
【解决方案2】:

我没有使用任何 sendgrid 的库,而是研究了他们在 api 网站上提供的 php 示例。这个独立的 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!');*/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 2014-08-24
    • 2015-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多