使用 SendGrid PHP 发送文件附件(sendgrid-php 库)
Send file attachment with SendGrid PHP (sendgrid-php library)
当我尝试使用 SendGrid PHP 库 (https://github.com/sendgrid/sendgrid-php) 发送附件时,函数失败(白屏)。删除 "setAttachment" 行使其再次工作。
这是我的代码:
require "sendgrid-php/sendgrid-php.php";
function sendgrid() {
$recips = array("me@mydomain.ca");
$categories = array("test");
$sendgrid = new SendGrid("API key removed");
$email = new SendGrid\Email();
$email
->setSmtpapiTos($recips)
->setFrom('mailroom@mydomain.ca')
->setSubject('Testing Sendgrid')
->setText('Hello World! Testing...')
->setHtml('<strong>Hello World!</strong>')
->setCategories($categories)
->setAttachment('test.txt')
;
//$sendgrid->send($email);
$res = $sendgrid->send($email);
var_dump($res);
}
sendgrid();
据我所知,我正在按照文档进行操作,但我想知道我是否没有正确设置文件路径的格式。 "Test.txt" 与包含上述代码的文件位于同一目录中。
谁能提供一些建议?
试试这个
->setAttachment(realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR.'test.txt');
这并不真正属于这里,但截至 2019 年 4 月 17 日,SendGrid 破坏了他们的 postfix
机制并在没有 "proper" 文档的情况下强制执行 v3 PHP 客户端。我的意思是:没有任何正确的记录,所以(因为当我输入 SendGrid 时,这个 post 出现在 google 的顶部 - 这里是)
所以如果你看到这个..不要惊慌这个post会帮助你
invalid authentication method - declined because you are using basic authentication with 2FA enabled. to fix, update to using an API key or disable 2FA and switch to using IP Access Management for security.
文档的一些问题
v3 上的 SendGrids 文档(在我看来)
- 声明性而非信息性(功能性声明而非技术信息)
- 严重错误(循环链接徒劳无功)
- 已过时(没有任何提醒)
- 缺少必要的组件(因此无法在没有提示的情况下开箱即用)....
例如:(只是一个味道)这里我们看到“they”(SendGrid)简单地取出\SendGrid\Email() 并在他们的代码库中将其替换为 \SendGrid\Mail\Mail() 但没有更新他们的文档 - 因此他们的 posted 示例将不起作用。 - 这是一个非常小的变化 - 但正如所写的那样,调用者是不同的,他们从未更新过他们的示例。他们省略了其他所有内容,这使得一件非常困难的事情变得容易。
即这个例子适用于 v3
$sendgrid = new SendGrid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
$email = new \SendGrid\Mail\Mail();
$email->setFrom("test@example.com", "Example User");
$email->setSubject("Sending with SendGrid is Fun");
$email->addTo("bigman@getme.com", "Example User");
$email->addContent(
"text/plain", "and easy to do anywhere, even with PHP"
);
$email->addContent(
"text/html", "<strong>and easy to do anywhere, even with PHP</strong>"
);
//optional (seems to be bullet proof, pdf, png, etc) to attach a file<START>
$att1 = new \SendGrid\Mail\Attachment();
$att1->setContent(file_get_contents("/path/to/some/test_attach.txt"));
$att1->setType("application/octet-stream");
$att1->setFilename(basename("/path/to/some/test_attach.txt"));
$att1->setDisposition("attachment");
$email->addAttachment($att1);
//optional to attach a file</END>
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";
}`
升级到 v3 SEND GRID PHP PHP 5.6
上没有作曲家的客户端
如果你像我一样讨厌作曲家(别让我开始)好消息是你可以得到SendGrid 工作如此 - 只是让你知道.. 几乎所有“他们”(SendGrid)引导你上网都很难帮助你
2019 年 4 月 17 日之后,SendGrid 表示您需要使用 v3 您将需要 PHP 5.6。(对或错 PHP 5.4 是上限redhat/centos 在撰写本文时)和 您不能再使用 postfix (尽管他们从未明确说明这一点,也没有标记他们的 postfix 示例 v2 max ,即自 2019 年 4 月 17 日起禁用)
截至 2019 年 4 月 17 日
-> you have to use v3
-> you have to use the PHP client... (no postfix)
-> you have to upgrade to 5.6
-> you **DO NOT** have to use composer (but if you don't you need to pay attention, see below)
另外 ->(重要)你在 v3 处被客户端或 curl 卡住了(他们不只是 说 这个) 没有 post 修复。算了吧。也回复:Curl 机制(虽然它确实适用于 5.4)带有 a)没有关于使用附件的文档和 b)使用非法 json_encoding PHP 无法输出(PHP 无法给出“{[ {}]}" 它总是 "{"0":[{}]}")
但不管怎样,您不仅必须在 5.6 PHP 使用 SendGrid PHP 客户端(再次不,服务器 post 修复选项)您仍然需要知道您需要两个项目:“php-http-client-master" and "sendgrid-php-master”(两个回购协议),这是未记录的:然后您需要执行以下操作:"sendgrid-php-master" 中的 /lib/loader.php
需要这两个最后添加的行
require_once '/your/path/to/php-http-client-master/lib/Client.php';
require_once '/your/path/to/php-http-client-master/lib/Response.php';
您还需要添加这个(遗憾的是 SendGrid 更加草率)
require_once __DIR__ . '/mail/TypeException.php';
顺便说一句,我也sendgrid-php-master\sendgrid-php.php
编辑了这个
<? php
require __DIR__ . '/lib/loader.php';
?>
理论上添加 composer 意味着您可以避免所有这些,但对于某些服务器来说这是不可能的(在我的 PHP 项目的不同分支上实施 composer 花了 3 周 - 我不再重复这个) 因为 composer 在 PHP 处理包含和 class 自动加载
的方式上引入了重大变化
PS:我已经提醒 SendGrid 所有这一切你可能会发现这很快就被清除了
当我尝试使用 SendGrid PHP 库 (https://github.com/sendgrid/sendgrid-php) 发送附件时,函数失败(白屏)。删除 "setAttachment" 行使其再次工作。
这是我的代码:
require "sendgrid-php/sendgrid-php.php";
function sendgrid() {
$recips = array("me@mydomain.ca");
$categories = array("test");
$sendgrid = new SendGrid("API key removed");
$email = new SendGrid\Email();
$email
->setSmtpapiTos($recips)
->setFrom('mailroom@mydomain.ca')
->setSubject('Testing Sendgrid')
->setText('Hello World! Testing...')
->setHtml('<strong>Hello World!</strong>')
->setCategories($categories)
->setAttachment('test.txt')
;
//$sendgrid->send($email);
$res = $sendgrid->send($email);
var_dump($res);
}
sendgrid();
据我所知,我正在按照文档进行操作,但我想知道我是否没有正确设置文件路径的格式。 "Test.txt" 与包含上述代码的文件位于同一目录中。
谁能提供一些建议?
试试这个
->setAttachment(realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR.'test.txt');
这并不真正属于这里,但截至 2019 年 4 月 17 日,SendGrid 破坏了他们的 postfix
机制并在没有 "proper" 文档的情况下强制执行 v3 PHP 客户端。我的意思是:没有任何正确的记录,所以(因为当我输入 SendGrid 时,这个 post 出现在 google 的顶部 - 这里是)
所以如果你看到这个..不要惊慌这个post会帮助你
invalid authentication method - declined because you are using basic authentication with 2FA enabled. to fix, update to using an API key or disable 2FA and switch to using IP Access Management for security.
文档的一些问题
v3 上的 SendGrids 文档(在我看来)
- 声明性而非信息性(功能性声明而非技术信息)
- 严重错误(循环链接徒劳无功)
- 已过时(没有任何提醒)
- 缺少必要的组件(因此无法在没有提示的情况下开箱即用)....
例如:(只是一个味道)这里我们看到“they”(SendGrid)简单地取出\SendGrid\Email() 并在他们的代码库中将其替换为 \SendGrid\Mail\Mail() 但没有更新他们的文档 - 因此他们的 posted 示例将不起作用。 - 这是一个非常小的变化 - 但正如所写的那样,调用者是不同的,他们从未更新过他们的示例。他们省略了其他所有内容,这使得一件非常困难的事情变得容易。
即这个例子适用于 v3
$sendgrid = new SendGrid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
$email = new \SendGrid\Mail\Mail();
$email->setFrom("test@example.com", "Example User");
$email->setSubject("Sending with SendGrid is Fun");
$email->addTo("bigman@getme.com", "Example User");
$email->addContent(
"text/plain", "and easy to do anywhere, even with PHP"
);
$email->addContent(
"text/html", "<strong>and easy to do anywhere, even with PHP</strong>"
);
//optional (seems to be bullet proof, pdf, png, etc) to attach a file<START>
$att1 = new \SendGrid\Mail\Attachment();
$att1->setContent(file_get_contents("/path/to/some/test_attach.txt"));
$att1->setType("application/octet-stream");
$att1->setFilename(basename("/path/to/some/test_attach.txt"));
$att1->setDisposition("attachment");
$email->addAttachment($att1);
//optional to attach a file</END>
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";
}`
升级到 v3 SEND GRID PHP PHP 5.6
上没有作曲家的客户端如果你像我一样讨厌作曲家(别让我开始)好消息是你可以得到SendGrid 工作如此 - 只是让你知道.. 几乎所有“他们”(SendGrid)引导你上网都很难帮助你
2019 年 4 月 17 日之后,SendGrid 表示您需要使用 v3 您将需要 PHP 5.6。(对或错 PHP 5.4 是上限redhat/centos 在撰写本文时)和 您不能再使用 postfix (尽管他们从未明确说明这一点,也没有标记他们的 postfix 示例 v2 max ,即自 2019 年 4 月 17 日起禁用)
截至 2019 年 4 月 17 日
-> you have to use v3
-> you have to use the PHP client... (no postfix)
-> you have to upgrade to 5.6
-> you **DO NOT** have to use composer (but if you don't you need to pay attention, see below)
另外 ->(重要)你在 v3 处被客户端或 curl 卡住了(他们不只是 说 这个) 没有 post 修复。算了吧。也回复:Curl 机制(虽然它确实适用于 5.4)带有 a)没有关于使用附件的文档和 b)使用非法 json_encoding PHP 无法输出(PHP 无法给出“{[ {}]}" 它总是 "{"0":[{}]}")
但不管怎样,您不仅必须在 5.6 PHP 使用 SendGrid PHP 客户端(再次不,服务器 post 修复选项)您仍然需要知道您需要两个项目:“php-http-client-master" and "sendgrid-php-master”(两个回购协议),这是未记录的:然后您需要执行以下操作:"sendgrid-php-master" 中的 /lib/loader.php
需要这两个最后添加的行
require_once '/your/path/to/php-http-client-master/lib/Client.php';
require_once '/your/path/to/php-http-client-master/lib/Response.php';
您还需要添加这个(遗憾的是 SendGrid 更加草率)
require_once __DIR__ . '/mail/TypeException.php';
顺便说一句,我也sendgrid-php-master\sendgrid-php.php
编辑了这个
<? php
require __DIR__ . '/lib/loader.php';
?>
理论上添加 composer 意味着您可以避免所有这些,但对于某些服务器来说这是不可能的(在我的 PHP 项目的不同分支上实施 composer 花了 3 周 - 我不再重复这个) 因为 composer 在 PHP 处理包含和 class 自动加载
的方式上引入了重大变化PS:我已经提醒 SendGrid 所有这一切你可能会发现这很快就被清除了