iamserjo/sendgrid-php

此库允许您使用PHP快速轻松地通过Twilio SendGrid发送电子邮件。

v7.5.0 2020-05-05 21:56 UTC

README

SendGrid Logo

BuildStatus Packagist Downloads Email Notifications Badge MIT licensed Twitter Follow GitHub contributors Open Source Helpers

NEW

此库允许您通过PHP快速轻松地使用Twilio SendGrid Web API v3。

此库的7.X.X版本提供了对所有Twilio SendGrid Web API v3端点的全面支持,包括新的v3 /mail/send

我们希望这个库是由社区驱动并由Twilio SendGrid领导的。您的帮助对于实现这一目标至关重要。为了确保我们按照正确的顺序构建正确的事情,我们要求您创建问题pull请求,或者简单地对现有的问题或pull请求进行投票或评论。

请浏览此README的其他部分以获取更多详细信息。

我们感谢您的持续支持,谢谢!

目录

安装

先决条件

  • PHP版本5.6,7.0,7.1,7.2,7.3或7.4
  • Twilio SendGrid服务,起始级别为免费,在前30天内可发送多达40,000封电子邮件,然后每天免费发送100封电子邮件,或者查看我们的定价
  • 对于短信消息,您需要一个免费的Twilio账户

设置环境变量

使用您的 SENDGRID_API_KEY 更新开发环境,例如

  1. 将示例env文件复制到新文件.env
cp .env.sample .env
  1. 编辑.env文件以包含您的SENDGRID_API_KEY
  2. .env文件
source ./.env

安装包

将Twilio SendGrid添加到您的composer.json文件。如果您没有使用Composer,我们强烈推荐它。它是管理PHP应用程序依赖项的绝佳方式。

{
  "require": {
    "sendgrid/sendgrid": "~7"
  }
}

替代方案:从zip文件安装包

如果您没有使用Composer,只需下载并安装库的最新打包版本(作为zip文件)

⬇︎ 下载打包库 ⬇︎

库的先前版本可以在版本索引中找到,或者直接从GitHub下载。

依赖关系

快速开始

Hello Email

以下是将电子邮件发送所需的最低代码。您可以在我们的USE_CASES文件中找到更多示例

<?php
require 'vendor/autoload.php'; // If you're using Composer (recommended)
// Comment out the above line if not using Composer
// require("<PATH TO>/sendgrid-php.php");
// If not using Composer, uncomment the above line and
// download sendgrid-php.zip from the latest release here,
// replacing <PATH TO> with the path to the sendgrid-php.php file,
// which is included in the download:
// https://github.com/sendgrid/sendgrid-php/releases

$email = new \SendGrid\Mail\Mail();
$email->setFrom("test@example.com", "Example User");
$email->setSubject("Sending with Twilio SendGrid is Fun");
$email->addTo("test@example.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>"
);
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
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";
}

SendGrid\Mail构造函数为您创建了一个个性化对象这里是一个如何向其中添加内容的示例。

通用v3 Web API使用(使用流畅接口)

<?php
require 'vendor/autoload.php'; // If you're using Composer (recommended)
// Comment out the above line if not using Composer
// Comment out the above line if not using Composer
// require("<PATH TO>/sendgrid-php.php");
// If not using Composer, uncomment the above line and
// download sendgrid-php.zip from the latest release here,
// replacing <PATH TO> with the path to the sendgrid-php.php file,
// which is included in the download:
// https://github.com/sendgrid/sendgrid-php/releases

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

try {
    $response = $sg->client->suppression()->bounces()->get();
    print $response->statusCode() . "\n";
    print_r($response->headers());
    print $response->body() . "\n";
} catch (Exception $e) {
    echo 'Caught exception: '.  $e->getMessage(). "\n";
}

通用v3 Web API使用(不使用流畅接口)

<?php
require 'vendor/autoload.php'; // If you're using Composer (recommended)
// Comment out the above line if not using Composer
// Comment out the above line if not using Composer
// require("<PATH TO>/sendgrid-php.php");
// If not using Composer, uncomment the above line and
// download sendgrid-php.zip from the latest release here,
// replacing <PATH TO> with the path to the sendgrid-php.php file,
// which is included in the download:
// https://github.com/sendgrid/sendgrid-php/releases

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

try {
    $response = $sg->client->_("suppression/bounces")->get();
    print $response->statusCode() . "\n";
    print_r($response->headers());
    print $response->body() . "\n";
} catch (Exception $e) {
    echo 'Caught exception: '.  $e->getMessage(). "\n";
}

用例

常见API用例示例,例如如何使用事务性模板发送电子邮件。

用法

公告

v7已发布!请查看发布说明以获取详细信息。

该库的所有更新都在我们的CHANGELOG发布中记录。您还可以订阅发布通知以获取发布和重大更改。

路线图

如果您对该项目的未来方向感兴趣,请查看我们的公开问题拉取请求。我们非常乐意听取您的反馈。

如何贡献

我们鼓励为我们的库做出贡献(您甚至可能获得一些酷炫的赠品),请参阅我们的CONTRIBUTING指南以获取详细信息。

快速链接

故障排除

请参阅我们的故障排除指南以了解常见的库问题。

关于

sendgrid-php由Twilio SendGrid,Inc维护和资助。sendgrid-php的名称和标志是Twilio SendGrid,Inc的商标。

如果您需要安装或使用库的帮助,请检查Twilio SendGrid支持帮助中心

如果您在库中发现了错误或想添加新功能,请继续为此仓库打开问题或拉取请求!

许可

MIT许可(MIT)