digitalcrm / newsletter
新闻通讯包
Requires
- php: ^7.2
- drewm/mailchimp-api: ^2.4
- illuminate/support: ^6.2
This package is auto-updated.
Last update: 2024-09-22 00:43:42 UTC
README
Install package
Create Controller, view, and rotues for this
Run
此包提供了一种简单的方法将MailChimp与Laravel "5.8.*"集成。
// at the top of your class use Newsletter; // ... Newsletter::subscribe('rincewind@discworld.com'); Newsletter::unsubscribe('the.luggage@discworld.com'); //Merge variables can be passed as the second argument Newsletter::subscribe('sam.vines@discworld.com', ['firstName'=>'Sam', 'lastName'=>'Vines']); //Subscribe someone to a specific list by using the third argument: Newsletter::subscribe('nanny.ogg@discworld.com', ['firstName'=>'Nanny', 'lastName'=>'Ogg'], 'Name of your list'); //Subscribe someone to a specific list and require them to confirm via email: Newsletter::subscribePending('nanny.ogg@discworld.com', ['firstName'=>'Nanny', 'lastName'=>'Ogg'], 'Name of your list'); //Subscribe or update someone Newsletter::subscribeOrUpdate('sam.vines@discworld.com', ['firstName'=>'Foo', 'lastName'=>'Bar']); // Change the email address of an existing subscriber Newsletter::updateEmailAddress('rincewind@discworld.com', 'the.luggage@discworld.com'); //Get some member info, returns an array described in the official docs Newsletter::getMember('lord.vetinari@discworld.com'); //Get the member activity, returns an array with recent activity for a given user Newsletter::getMemberActivity('lord.vetinari@discworld.com'); //Get the members for a given list, optionally filtered by passing a second array of parameters Newsletter::getMembers(); //Check if a member is subscribed to a list Newsletter::isSubscribed('rincewind@discworld.com'); //Returns a boolean Newsletter::hasMember('greebo@discworld.com'); // Get the tags for a member in a given list Newsletter::getTags('lord.vetinari@discworld.com'); // Add tags for a member in a given list, any new tags will be created Newsletter::addTags(['tag-1', 'tag-2'], 'lord.vetinari@discworld.com'); // Remove tags for a member in a given list Newsletter::removeTags(['tag-1', 'tag-2'], 'lord.vetinari@discworld.com'); //If you want to do something else, you can get an instance of the underlying API: Newsletter::getApi();
安装
您可以通过以下方式使用Composer安装此包:
composer require digitalcrm/newsletter
此包将自动注册自己。
要发布配置文件到config/newsletter.php,请运行:
php artisan vendor:publish --provider="Digitalcrm\Newsletter\NewsletterServiceProvider"
这将发布一个文件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('MAILCHIMP_DRIVER', 'api'), /* * The API key of a MailChimp account. You can find yours at * https://us10.admin.mailchimp.com/account/api-key-popup/. */ 'apiKey' => env('MAILCHIMP_APIKEY'), /* * The listName to use when no listName has been specified in a method. */ 'defaultListName' => 'subscribers', /* * Here you can define properties of the lists. */ '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' => [ /* * A MailChimp list id. Check the MailChimp docs if you don't know * how to get this value: * http://kb.mailchimp.com/lists/managing-subscribers/find-your-list-id. */ 'id' => env('MAILCHIMP_LIST_ID'), ], ], /* * If you're having trouble with https connections, set this to false. */ 'ssl' => true, ];
使用方法
在安装了此包并填写了配置文件中的值之后,使用此包将变得非常容易。以下所有示例都使用外观。不要忘记在文件顶部导入它。
use Newsletter;
订阅、更新和取消订阅
可以这样订阅一个电子邮件地址:
use Newsletter; Newsletter::subscribe('rincewind@discworld.com');
让我们取消某人的订阅
Newsletter::unsubscribe('the.luggage@discworld.com');
您可以将一些合并变量作为第二个参数传递
Newsletter::subscribe('rincewind@discworld.com', ['firstName'=>'Rince', 'lastName'=>'Wind']);
请注意,在撰写本文时,MailChimp的默认合并变量名称为
FNAME和LNAME。在我们的示例中,我们使用firstName和lastName以提高可读性。
您可以使用第三个参数将某人订阅到特定的列表
Newsletter::subscribe('rincewind@discworld.com', ['firstName'=>'Rince', 'lastName'=>'Wind'], 'subscribers');
第三个参数是配置文件中配置的列表名称。
您也可以订阅和/或更新某人。如果该人已经订阅,则将订阅或更新该人
Newsletter::subscribeOrUpdate('rincewind@discworld.com', ['firstName'=>'Foo', 'lastname'=>'Bar']);
您可以使用第四个参数将某人订阅到一个或多个特定的组/兴趣
Newsletter::subscribeOrUpdate('rincewind@dscworld.com', ['firstName'=>'Rince','lastName'=>'Wind'], 'subscribers', ['interests'=>['interestId'=>true, 'interestId'=>true]])
如果想要从组/兴趣中删除某人,只需添加false
您还可以从特定列表中取消某人的订阅
Newsletter::unsubscribe('rincewind@discworld.com', 'subscribers');
删除订阅者
删除与取消订阅不同。与取消订阅不同,删除成员会导致丢失所有历史记录(添加/选择/编辑)以及从列表中删除他们。在大多数情况下,您想要使用unsubscribe而不是delete。
以下是执行删除的方法
Newsletter::delete('rincewind@discworld.com');
永久删除订阅者
删除与列表成员相关的所有个人可识别信息,并将他们从列表中删除。这将使得无法重新导入列表成员。
以下是执行永久删除的方法
Newsletter::deletePermanently('rincewind@discworld.com');
获取订阅者信息
您可以使用getMember函数获取有关订阅者的信息
Newsletter::getMember('lord.vetinari@discworld.com');
这将返回一个包含订阅者信息的数组。如果没有通过该电子邮件地址订阅的人,该函数将返回false
还有一个方便的方法来检查某人是否已经订阅
Newsletter::hasMember('nanny.ogg@discworld.com'); //returns a boolean
此外,您还可以检查用户是否已订阅您的列表
Newsletter::isSubscribed('lord.vetinari@discworld.com'); //returns a boolean
创建活动
这是createCampaign的签名
public function createCampaign( string $fromName, string $replyTo, string $subject, string $html = '', string $listName = '', array $options = [], array $contentOptions = [])
请注意,此活动仅被创建,不会发送任何邮件。
处理错误
如果出现问题,您可以使用以下方法获取最后一个错误:
Newsletter::getLastError();
如果您只想确保最后一个操作是否成功,可以使用以下方法:
Newsletter::lastActionSucceeded(); //returns a boolean
还需要其他东西吗?
如果您需要更多功能,您可以通过以下方式获取底层MailChimp Api的实例:
$api = Newsletter::getApi();