rodchyn/sendgrid-php-sdk

Sendgrid PHP SDK

dev-master 2012-06-19 15:17 UTC

This package is auto-updated.

Last update: 2024-08-29 04:02:55 UTC


README

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

许可证

根据MIT许可证授权。

安装

git clone git@github.com:sendgrid/sendgrid-php.git

SendGrid API

SendGrid提供两种发送电子邮件的方法:Web API和SMTP API。SendGrid建议使用SMTP API发送电子邮件。有关每种方法优势的解释,请参阅 http://docs.sendgrid.com/documentation/get-started/integrate/examples/smtp-vs-rest/

此库实现了一个通用接口,使得使用任一API都非常简单。

邮件使用前

在开始使用此库之前,了解一些关于库架构的要点很重要...

  • SendGrid邮件对象是用来设置邮件数据的方式。通常,大多数元素的数据可以通过以下三种方式设置

    1. set - 重置数据,并将其初始化为给定元素。这将销毁之前的数据
    2. set (List) - 对于基于数组的元素,我们提供了一种一次性传入整个数组的方法。这也会销毁之前的数据。
    3. add - 将数据添加到元素列表中。
  • 发送电子邮件就像

    1. 创建SendGrid实例
    2. 创建SendGrid邮件对象,并设置其数据
    3. 使用SMTP API或Web API发送邮件。

邮件使用

要开始使用此库,您必须首先包含它

include 'path/to/sendgrid-php/SendGrid_loader.php';

然后,使用您的SendGrid凭据初始化SendGrid对象

$sendgrid = new SendGrid('username', 'password');

创建一个新的SendGrid邮件对象并添加您的消息详情

$mail = new SendGrid\Mail();
$mail->addTo('foo@bar.com')->
       setFrom('me@bar.com')->
       setSubject('Subject goes here')->
       setText('Hello World!')->
       setHtml('<strong>Hello World!</strong>');

使用您选择的API(SMTP或Web)发送它

$sendgrid->smtp->send($mail);

或者

$sendgrid->web->send($mail);

使用类别

类别用于将SendGrid提供的电子邮件统计分组。

要使用类别,只需设置类别名称。注意:每封电子邮件最多可以有10个类别。

$mail = new SendGrid\Mail();
$mail->addTo('foo@bar.com')->
       ...
       addCategory("Category 1")->
       addCategory("Category 2");

使用附件

附件目前仅基于文件,未来计划还包括内存实现。

文件附件大小限制为每文件7 MB。

$mail = new SendGrid\Mail();
$mail->addTo('foo@bar.com')->
       ...
       addAttachment("../path/to/file.txt");    

使用替换

替换可用于自定义多收件人电子邮件,并为用户量身定制。

$mail = new SendGrid\Mail();
$mail->addTo('john@somewhere.com')->
       addTo("harry@somewhere.com")->
       addTo("Bob@somewhere.com")->
       ...
       setHtml("Hey %name%, we've seen that you've been gone for a while")->
       addSubstitution("%name%", array("John", "Harry", "Bob"));

使用部分

部分可用于进一步定制针对最终用户的消息。部分仅与替换值一起使用时才有用。

$mail = new SendGrid\Mail();
$mail->addTo('john@somewhere.com')->
       addTo("harry@somewhere.com")->
       addTo("Bob@somewhere.com")->
       ...
       setHtml("Hey %name%, you work at %place%")->
       addSubstitution("%name%", array("John", "Harry", "Bob"))->
       addSubstitution("%place%", array("%office%", "%office%", "%home%"))->
       addSection("%office%", "an office")->
       addSection("%home%", "your house");

使用唯一参数

唯一参数用于跟踪目的

$mail = new SendGrid\Mail();
$mail->addTo('foo@bar.com')->
       ...
       addUniqueArgument("Customer", "Someone")->
       addUniqueArgument("location", "Somewhere");

使用过滤器设置

过滤器设置用于启用和禁用应用程序,并向这些应用程序传递参数。

$mail = new SendGrid\Mail();
$mail->addTo('foo@bar.com')->
       ...
       addFilterSetting("gravatar", "enable", 1)->
       addFilterSetting("footer", "enable", 1)->
       addFilterSetting("footer", "text/plain", "Here is a plain text footer")->
       addFilterSetting("footer", "text/html", "<p style='color:red;'>Here is an HTML footer</p>");

使用头部

头部可用于添加现有的sendgrid功能(如类别或过滤器),或在必要时添加自定义头部。

$mail = new SendGrid\Mail();
$mail->addTo('foo@bar.com')->
       ...
       addHeader("category", "My New Category");