微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

可恢复的致命错误:类 SendGrid\Mail\Mail 的对象无法转换为字符串

如何解决可恢复的致命错误:类 SendGrid\Mail\Mail 的对象无法转换为字符串

我正在尝试使用 SendGrid 的 PHP 库传递动态值。当我传入一个字符串但我添加一个动态值时它起作用,我收到这个错误 可恢复的致命错误:第 30 行上的类 SendGrid\Mail\Mail 的对象无法转换为 C:\xampp\htdocs\new\vendor\sendgrid\sendgrid\lib\helper\Assert.PHP 中的字符串

这个有效

$email = new \SendGrid\Mail\Mail(); 
$email->setFrom("support@example.com","example");
$email->setSubject("IMPORTANT: Signal failure");
$email->addTo("user@exampl.com","{$fname}");
$email->addContent("text/plain","{$message}");
$email->addContent(
    "text/html","{$message}"
);
$sendgrid = new \SendGrid('APIKEY');
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";
}

这个失败了

 $email = new \SendGrid\Mail\Mail(); 
    $email->setFrom("support@example.com","example");
    $email->setSubject("IMPORTANT: Signal failure");
    $email->addTo("{$email}","{$fname}"); //this line causes the error
    $email->addContent("text/plain","{$message}");
    $email->addContent(
        "text/html","{$message}"
    );
    $sendgrid = new \SendGrid('APIKEY');
    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";
    }

任何建议将不胜感激 谢谢

解决方法

在失败的示例中,$email 不是包含电子邮件地址的字符串。它是 \SendGrid\Mail\Mail() 的一个实例。

将其作为字符串调用是行不通的,因为看起来 \SendGrid\Mail\Mail() 没有实现 Stringable interface

您可以通过使用更好的变量名称来防止此类事故,例如

$mailer = new \SendGrid\Mail\Mail();

$sendGridMail = new \SendGrid\Mail\Mail();

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。