【问题标题】:cakephp 3.2 email with sendgrid issue带有 sendgrid 问题的 cakephp 3.2 电子邮件
【发布时间】:2016-03-01 15:14:43
【问题描述】:

我很难弄清楚如何使用 sendgrid 发送邮件。

这是我目前拥有的代码:

员工控制者:

function _sendEmail($id) {
    $email = new Email();
        try {
            $email->from(['coms@me.co' => 'Me'])
                ->profile('SendGrid')
                ->to([$id['email'] => $id['full_name']])
                ->subject("TEST SUBJECT")
                ->emailFormat("both")
                ->template('default')
                ->send('My message');
            $this->Flash->success("Message sent.");
        } catch (Exception $ex) {
            echo 'Exception : ', $ex->getMessage(), "\n";
        }                              
        return $this->redirect(['action' => 'index']);
}

我正在使用几天前发现的这个插件; https://github.com/Iandenh/cakephp-sendgrid... 我按照文档中的说明配置了所有内容,但是当我想发送邮件时,没有任何反应,该函数会闪烁成功消息并进行重定向,但没有发送电子邮件。

这是 app.php 中的电子邮件传输

'EmailTransport' => [
    'SendGridEmail' => [
        'className' => 'SendGridEmail.SendGrid',
        'api_key' => 'API_KEY'
    ],

和交付配置文件

'Email' => [
    'SendGrid' => [
        'transport' => 'SendGridEmail',
        'template' => 'default',
        'layout' => 'default',
        'emailFormat' => 'both',
        'from' => ['coms@me.co' => 'Me'],
        'sender' => ['coms@me.co' => 'Me'],
    ]
]

如果有人能指出我的任何错误或可能的解决方案,我将不胜感激。

【问题讨论】:

  • 您在将 API 密钥等从配置文件中删除之前,是否已将其放在这里?也许是一个愚蠢的问题,但只是确保。
  • 哈哈是的,我在发帖前删除了它。
  • 插件传输似乎吞噬了 SendGrid API 响应,因此您永远不会知道 API 调用会发生什么。您可能想要求开发人员对该问题进行增强,和/或根据需要扩展传输添加功能。另外请务必查看 Webhooks,因为 API 调用只通知 API 调用本身是否成功,而不是发送邮件是否成功。顺便说一句,为什么不使用 SendGrid SMTP 中继?

标签: php email cakephp-3.0 sendgrid


【解决方案1】:

您好,这可能为时已晚,但以防万一有人遇到同样的问题。

这对我有用

所以你犯的第一个错误是你使用的是 Email 类,而 sendgrid 你现在应该使用 sendgrid Mail 方法

如果不继续,我假设你已经安装了这个包并将它添加到 composer.json 文件并更新 composer

 "sendgrid/sendgrid": "~7",

之后,您可以使用 sendgrid 类发送电子邮件,如下例所示

    protected function sendEmail($to, $subject, $content)
{
    $email = new \SendGrid\Mail\Mail();
    $email->setFrom(Configure::consume('App.from_email'), Configure::consume('Theme.title'));
    $email->setSubject($subject);
    $email->addTo($to);
    $email->addContent("text/html", 'Your email body');
    $sendgrid = new \SendGrid(Configure::consume('App.sendgrid_api_key'));
    try {
        $response = $sendgrid->send($email);
        print $response->statusCode() . "\n";
        print_r($response->headers());
        print $response->body() . "\n";
    } catch (Exception $e) {
        echo 'Caught exception: '. $e->getMessage() ."\n";
    }
}

您需要将 sendgrid_api_key 替换为您的 api 密钥。我在我的配置文件中使用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-15
    • 2011-08-28
    • 2012-06-03
    • 1970-01-01
    • 1970-01-01
    • 2017-12-26
    • 2011-01-02
    • 1970-01-01
    相关资源
    最近更新 更多