为什么sendgrid在我尝试发送电子邮件时向我显示错误消息?

时间:2017-05-28 11:48:39

标签: php sendgrid

我使用以下代码使用sendgrid发送电子邮件:

<?php
require 'sendgrid-php/vendor/autoload.php';
$sendgrid = new SendGrid("my_key");
$email    = new SendGrid\Email();

$email->addTo("to@gmail.com")
      ->setFrom("from@youremail.com")
      ->setSubject("Sending with SendGrid is Fun")
      ->setHtml("and easy to do anywhere, even with PHP");

$sendgrid->send($email);
?>

当我在代码上面运行时,它会向我显示一条错误消息,如下所示:

  

致命错误:未捕获ArgumentCountError:参数太少   函数SendGrid \ Email :: __ construct(),传入0   第5行和第3行的C:\ xampp \ htdocs \ freelancer \ mysite \ test-email.php   预计正好2个   C:\ XAMPP \ htdocs中\自由职业者\ mysite的\ sendgrid的PHP \ LIB \佣工\邮件\ Mail.php:851   堆栈跟踪:#0   C:\ XAMPP \ htdocs中\自由职业者\ mysite的\测试email.php(5):   抛出了SendGrid \ Email-&gt; __ construct()#1 {main}   C:\ XAMPP \ htdocs中\自由职业者\ mysite的\ sendgrid的PHP \ LIB \佣工\邮件\ Mail.php   在851线上

我不明白我错过了什么?

更新一次:

require("sendgrid-php/sendgrid-php.php");
$from = new SendGrid\Email("Example User", "from@gmail.com");
$subject = "Sending with SendGrid is Fun";
$to = new SendGrid\Email("Example User", "to@gmail.com");
$content = new SendGrid\Content("text/plain", "and easy to do anywhere, even with PHP");
$mail = new SendGrid\Mail($from, $subject, $to, $content);
$apiKey = getenv('my-key');
$sg = new \SendGrid($apiKey);
$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
echo '<pre>';
print_r($response->headers());
echo '</pre>';
echo $response->body();

现在我正在使用上述方法,但它正在向我展示

  

{&#34;错误&#34;:[{&#34;消息&#34;:&#34;权限被拒绝,错误   凭证&#34;&#34;字段&#34;:空,&#34;帮助&#34;:空}]}

0 个答案:

没有答案