Php 使用wadeshuler/yii2 sendgrid发送sendgrid电子邮件时出现错误请求

Php 使用wadeshuler/yii2 sendgrid发送sendgrid电子邮件时出现错误请求,php,yii2,sendgrid-api-v3,Php,Yii2,Sendgrid Api V3,我目前正在开发一个yii2应用程序,我已经在我的应用程序中集成了wadeshuler/yii2 sendgrid插件,通过它发送电子邮件 我已在sendgrid帐户中使用模板id设置了动态模板。下面是我用来测试sendgrid与我的应用程序集成的代码 public function actionTest() { $mailer = Yii::$app->mailer; $message = $mailer->compose() ->setTo('

我目前正在开发一个yii2应用程序,我已经在我的应用程序中集成了wadeshuler/yii2 sendgrid插件,通过它发送电子邮件

我已在sendgrid帐户中使用模板id设置了动态模板。下面是我用来测试sendgrid与我的应用程序集成的代码

public function actionTest()
{
    $mailer = Yii::$app->mailer;
    $message = $mailer->compose()
        ->setTo('umair@****.com')      // or just $user->email
        ->setFrom(['alerts@example.com' => 'Alerts'])
        ->setReplyTo('noreply@example.com')
        ->setSubject('Hey -username-, Read This Email')
        ->setHtmlBody('Dear -username-,<br><br>My HTML message here')
        ->setTextBody('Dear -username-,\n\nMy Text message here')
        ->setTemplateId('******')

        ->addSubstitution('-username-', 'Umair Ashraf')
        ->send();

    if ($message === true) {
        echo 'Success!';
        echo '<pre>' . print_r($mailer->getRawResponses(), true) . '</pre>';
    } else {
        echo 'Error!<br>';
        echo '<pre>' . print_r($mailer, true) . '</pre>';
        echo '<pre>' . print_r($mailer->getErrors(), true) . '</pre>';
    }

}
public function actionTest()
{
$mailer=Yii::$app->mailer;
$message=$mailer->compose()
->设置为('umair@****.com')//或仅$user->email
->setFrom(['alerts@example.com“=>”警报“])
->setReplyTo('noreply@example.com')
->setSubject('Hey-username-,阅读此电子邮件')
->setHtmlBody('亲爱的-用户名-,

我的HTML消息在这里') ->setTextBody('亲爱的-用户名-,\n\n我的短信在这里') ->setTemplateId('******') ->addSubstitution('-username-',Umair Ashraf') ->send(); 如果($message==true){ 呼应‘成功!’;
public function email($from , $to, $substitution, $templateId, $senderName = NULL, 
$toName = NULL, $attachment = NULL)
{
    $response = '';
    $email = new \SendGrid\Mail\Mail();
    $email->setFrom($from,$senderName);
    $email->addTo($to , $toName, $substitution);
    $email->setTemplateId($templateId);
    if(!empty($attachment))
    {
        $email->addAttachment(
            $attachment
        );
    }
    $sendgrid = new \SendGrid($this->apiKey);

    try {
        $response = $sendgrid->send($email);

    } catch (Exception $e) {
        echo 'Caught exception: '.  $e->getMessage(). "\n";
    }

    return $response;
}
回显“”。打印($mailer->getRawResponses(),true)。“”;
public function email($from , $to, $substitution, $templateId, $senderName = NULL, 
$toName = NULL, $attachment = NULL)
{
    $response = '';
    $email = new \SendGrid\Mail\Mail();
    $email->setFrom($from,$senderName);
    $email->addTo($to , $toName, $substitution);
    $email->setTemplateId($templateId);
    if(!empty($attachment))
    {
        $email->addAttachment(
            $attachment
        );
    }
    $sendgrid = new \SendGrid($this->apiKey);

    try {
        $response = $sendgrid->send($email);

    } catch (Exception $e) {
        echo 'Caught exception: '.  $e->getMessage(). "\n";
    }

    return $response;
}
}否则{ 回显“错误!
”;
public function email($from , $to, $substitution, $templateId, $senderName = NULL, 
$toName = NULL, $attachment = NULL)
{
    $response = '';
    $email = new \SendGrid\Mail\Mail();
    $email->setFrom($from,$senderName);
    $email->addTo($to , $toName, $substitution);
    $email->setTemplateId($templateId);
    if(!empty($attachment))
    {
        $email->addAttachment(
            $attachment
        );
    }
    $sendgrid = new \SendGrid($this->apiKey);

    try {
        $response = $sendgrid->send($email);

    } catch (Exception $e) {
        echo 'Caught exception: '.  $e->getMessage(). "\n";
    }

    return $response;
}
回显“”。打印($mailer,true)。“”;
public function email($from , $to, $substitution, $templateId, $senderName = NULL, 
$toName = NULL, $attachment = NULL)
{
    $response = '';
    $email = new \SendGrid\Mail\Mail();
    $email->setFrom($from,$senderName);
    $email->addTo($to , $toName, $substitution);
    $email->setTemplateId($templateId);
    if(!empty($attachment))
    {
        $email->addAttachment(
            $attachment
        );
    }
    $sendgrid = new \SendGrid($this->apiKey);

    try {
        $response = $sendgrid->send($email);

    } catch (Exception $e) {
        echo 'Caught exception: '.  $e->getMessage(). "\n";
    }

    return $response;
}
回显“”。打印($mailer->getErrors(),true)。“”;
public function email($from , $to, $substitution, $templateId, $senderName = NULL, 
$toName = NULL, $attachment = NULL)
{
    $response = '';
    $email = new \SendGrid\Mail\Mail();
    $email->setFrom($from,$senderName);
    $email->addTo($to , $toName, $substitution);
    $email->setTemplateId($templateId);
    if(!empty($attachment))
    {
        $email->addAttachment(
            $attachment
        );
    }
    $sendgrid = new \SendGrid($this->apiKey);

    try {
        $response = $sendgrid->send($email);

    } catch (Exception $e) {
        echo 'Caught exception: '.  $e->getMessage(). "\n";
    }

    return $response;
}
} }
这是打印的响应代码

错误! {“code”:400,“headers”:[“HTTP/1.1 400错误请求”,“服务器:nginx”,“日期:Fri,2019年12月27日13:31:27 GMT”,“内容类型:application/json”,“内容长度:238”,“连接:保持活动状态”,“访问控制允许来源:”,“访问控制允许方法:POST”,“访问控制允许头部:授权,内容类型,代表x-sg-elas-acl”,“访问控制最大年龄:600”,“X-No-CORS-Reason:”,“,”,“],“正文”:“{”错误“:[{”消息“:\”替换不能与动态模板一起使用“,”字段“:\”个性化.0.替换“,”帮助“:\”:\”}”

排列 ( [0]=>请求不正确! )


正如你所看到的,替换可能不用于动态模板,但我需要替换这些变量,以便正确使用模板。如何使用代码修复此问题?

我不得不删除wadeshuler/yii2 sendgrid插件,因为它不支持sendgrid库中的最新功能和更新。我安装了最新的sendgrid插件。我还制作了一个名为SendGridManager的组件,用于在我的应用程序中路由所有电子邮件

下面是我在组件SendgridManager中的函数

阅读此内容以获取修复此错误的详细信息