spatie / laravel-newsletter
在Laravel中管理Mailcoach和MailChimp新闻通讯
Requires
- php: ^8.1
- illuminate/log: ^9|^10|^11
- spatie/laravel-package-tools: ^1.13
Requires (Dev)
- drewm/mailchimp-api: ^2.5
- guzzlehttp/guzzle: ^7.5|^7.2
- mockery/mockery: ^1.4
- orchestra/testbench: ^7.11|^8.0
- pestphp/pest: ^1.20
- phpstan/phpstan: ^1.9
- phpunit/phpunit: ^9.5
- spatie/mailcoach-sdk-php: ^1.0
- spatie/ray: ^1.28
Suggests
- drewm/mailchimp-api: For working with MailChimp
- spatie/mailcoach-sdk-php: For working with Mailcoach
- dev-main
- 5.2.0
- 5.1.1
- 5.1.0
- 5.0.0
- v4.x-dev
- 4.11.0
- 4.10.0
- 4.9.0
- 4.8.2
- 4.8.1
- 4.8.0
- 4.7.1
- 4.7.0
- 4.6.0
- 4.5.0
- 4.4.1
- 4.4.0
- 4.3.0
- 4.2.3
- 4.2.2
- 4.2.1
- 4.2.0
- 4.1.0
- 4.0.0
- 3.7.0
- 3.6.1
- 3.6.0
- 3.5.0
- 3.4.0
- 3.3.0
- 3.2.0
- 3.1.0
- 3.0.6
- 3.0.5
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.2.0
- 2.1.0
- 2.0.0
- 1.1.0
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- 0.0.1
This package is auto-updated.
Last update: 2024-09-18 15:19:14 UTC
README
此包提供了一种简单的方法来集成各种电子邮件服务的电子邮件列表订阅。
目前此包支持
支持我们
我们投入了大量资源来创建 一流的开放源代码包。您可以通过 购买我们的付费产品之一 来支持我们。
我们非常感谢您从家乡寄给我们一张明信片,说明您正在使用我们的哪个包。您可以在 我们的联系页面 上找到我们的地址。我们将发布所有收到的明信片在我们的 虚拟明信片墙 上。
安装
您可以使用Composer通过以下方式安装此包
composer require spatie/laravel-newsletter
要发布配置文件到 config/newsletter.php
运行
php artisan vendor:publish --tag="newsletter-config"
这将发布一个位于您配置目录中的 newsletter.php
文件,内容如下
return [ /* * The driver to use to interact with MailChimp API. * You may use "log" or "null" to prevent calling the * API directly from your environment. */ 'driver' => env('NEWSLETTER_DRIVER', Spatie\Newsletter\Drivers\MailcoachDriver::class), /** * These arguments will be given to the driver. */ 'driver_arguments' => [ 'api_key' => env('NEWSLETTER_API_KEY'), 'endpoint' => env('NEWSLETTER_ENDPOINT'), ], /* * The list name to use when no list name is specified in a method. */ 'default_list_name' => 'subscribers', 'lists' => [ /* * This key is used to identify this list. It can be used * as the listName parameter provided in the various methods. * * You can set it to any string you want and you can add * as many lists as you want. */ 'subscribers' => [ /* * When using the Mailcoach driver, this should be the Email list UUID * which is displayed in the Mailcoach UI * * When using the MailChimp driver, this should be a MailChimp list id. * http://kb.mailchimp.com/lists/managing-subscribers/find-your-list-id. */ 'id' => env('NEWSLETTER_LIST_ID'), ], ], ];
使用Mailcoach
要让此包与Mailcoach一起工作,您需要安装Mailcoach SDK。
composer require spatie/mailcoach-sdk-php
接下来,您必须在配置文件中提供API密钥、端点和 list.subscribers.id
的值。您可以在 Mailcoach 设置屏幕中找到API密钥和端点。 list.subscribers.id
的值必须是Mailcoach上电子邮件列表的UUID。您可以在电子邮件列表的设置屏幕中找到此值
使用MailChimp
要使用MailChimp,请安装此额外包。
composer require drewm/mailchimp-api
配置文件中 newsletter
的 driver
键必须设置为 Spatie\Newsletter\Drivers\MailChimpDriver::class
。
接下来,您必须在配置文件中提供API密钥和 list.subscribers.id
的值。您可以在MailChimp UI中找到这些值。
配置值 endpoint
必须设置为null。
用法
安装完包并在配置文件中填写值后,使用此包将变得非常简单。以下所有示例都使用外观。别忘了在文件顶部导入它。
use Spatie\Newsletter\Facades\Newsletter;
订阅、更新和取消订阅
以这种方式订阅电子邮件地址
use Newsletter; Newsletter::subscribe('rincewind@discworld.com');
让我们取消某人的订阅
Newsletter::unsubscribe('the.luggage@discworld.com');
对于Mailcoach,您可以将额外的属性作为第二个参数传递
Newsletter::subscribe('rincewind@discworld.com', ['first_name' => 'Rince', 'last_name' => 'Wind']);
对于MailChimp,您可以将合并变量作为第二个参数传递
Newsletter::subscribe('rincewind@discworld.com', ['FNAME'=>'Rince', 'LNAME'=>'Wind']);
您可以通过传递一个列表名称来将某人订阅到特定列表
Newsletter::subscribe('rincewind@discworld.com', listName: 'subscribers');
第三个参数是配置文件中配置的列表名称。
您还可以订阅和/或更新某人。如果该人已经订阅,则将订阅或更新此人
Newsletter::subscribeOrUpdate('rincewind@discworld.com', ['first_name' => 'Rince', 'last_name' => 'Wind']);
对于MailChimp,您可以通过使用第四个参数将某人订阅到一个或多个特定的组/兴趣
Newsletter::subscribeOrUpdate( 'rincewind@dscworld.com', ['FNAME'=>'Rince','LNAME'=>'Wind'], 'subscribers', ['interests'=>['interestId'=>true, 'interestId'=>true]], );
如果要将某人从组/兴趣中删除,请简单地将它添加为 false
。
以下是取消某人对特定列表订阅的示例
Newsletter::unsubscribe('rincewind@discworld.com', 'subscribers');
删除订阅者
删除与退订不同。与退订不同,删除成员将导致所有历史记录(添加/订阅/编辑)的丢失,并从列表中移除他们。在大多数情况下,您应该使用退订
而不是删除
。
以下是执行删除操作的方法
Newsletter::delete('rincewind@discworld.com');
获取订阅者信息
您可以通过使用getMember
函数来获取订阅者的信息
Newsletter::getMember('lord.vetinari@discworld.com');
对于MailCoach,这将返回一个Spatie\Mailcoach\Resources|Subscriber
的实例。对于MailChimp,这将返回一个包含订阅者信息的数组。
如果没有人使用该电子邮件地址订阅,该函数将返回false
还有一个方便的方法来检查某人是否已经订阅
Newsletter::hasMember('nanny.ogg@discworld.com'); //returns a boolean
此外,您还可以检查用户是否已订阅您的列表
Newsletter::isSubscribed('lord.vetinari@discworld.com'); //returns a boolean
还需要其他什么吗?
如果您需要更多功能,可以通过以下方式获取底层API的实例
$api = Newsletter::getApi();
如果您在获取MailChimp集成时遇到困难,您可以使用以下方式查看最后一个错误
Newsletter::getApi()->getLastError();
测试
使用以下命令运行测试
vendor/bin/pest
变更日志
请参阅变更日志以获取有关最近更改的更多信息。
贡献
请参阅贡献指南以获取详细信息。
安全
如果您发现任何安全相关的问题,请通过电子邮件security@spatie.be而不是使用问题跟踪器来报告。
鸣谢
- Freek Van der Herten
- 所有贡献者致力于在维护和新酷功能上分配人力。
许可
MIT许可(MIT)。请参阅许可文件以获取更多信息。