首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SendGrid:缺少SendGrid\Email::__construct()的参数1

SendGrid:缺少SendGrid\Email::__construct()的参数1
EN

Stack Overflow用户
提问于 2016-08-04 02:49:41
回答 2查看 1.3K关注 0票数 0

我确信这是一个简单的解决方案,我正在尝试设置一个sendgrid来发送确认电子邮件。

代码语言:javascript
复制
require 'sendgrid-php/vendor/autoload.php';
$sendgrid = new SendGrid($user,$pass);
$email    = new SendGrid\Email();

$email->addTo($sEmail)
      ->setFrom($email)
      ->setSubject("Sending with SendGrid is Fun")
      ->setHtml("and easy to do anywhere, even with PHP");

$sendgrid->send($email);

在执行代码时,我得到以下错误消息:

代码语言:javascript
复制
 Warning: Missing argument 1 for SendGrid\Email::__construct()

我正在运行PHP版本5.6.16

我确信我错过了一些愚蠢的事情。

EN

回答 2

Stack Overflow用户

发布于 2016-08-04 20:14:42

SendGrid-PHP库有一些更新。请参阅自述文件中的示例:https://github.com/sendgrid/sendgrid-php

代码语言:javascript
复制
<?php
// If you are using Composer
require 'vendor/autoload.php';

// If you are not using Composer (recommended)
// require("path/to/sendgrid-php/sendgrid-php.php");

$from = new SendGrid\Email(null, "test@example.com");
$subject = "Hello World from the SendGrid PHP Library!";
$to = new SendGrid\Email(null, "test@example.com");
$content = new SendGrid\Content("text/plain", "Hello, Email!");
$mail = new SendGrid\Mail($from, $subject, $to, $content);

$apiKey = getenv('SENDGRID_API_KEY');
$sg = new \SendGrid($apiKey);

$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
echo $response->headers();
echo $response->body();

你得到这个错误是因为$email = new SendGrid\Email();需要两个参数,就像这里看到的,https://github.com/sendgrid/sendgrid-php/blob/master/lib/helpers/mail/Mail.php#L845$name$email

$name如果你不想要发件人名字,你可以离开null

此外,像$sendgrid = new SendGrid($user,$pass);这样的用户名和密码的使用也被弃用,请参阅此处:https://github.com/sendgrid/sendgrid-php/blob/master/lib/SendGrid.php#L34

您需要创建一个API密钥并使用它。查看此处:https://app.sendgrid.com/settings/api_keys

票数 2
EN

Stack Overflow用户

发布于 2017-02-10 15:30:45

我也在为send grid处理类似的问题。

我通过添加下面的头来让它工作。

"Content-Type":“应用程序/json;charset=utf8”

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38751471

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档