thedigit/laravel5.7-newsletter

在 Laravel 中管理通讯录

4.2.1 2018-03-10 07:53 UTC

README

Latest Version Software License Build Status Quality Score StyleCI Total Downloads

此包提供了一种将 MailChimp 集成到 Laravel 5 的简便方法。在幕后使用 MailChimp API 的 v3。以下是您可以使用此包做的一些示例

请注意,在撰写本文时,MailChimp 的默认合并变量名为 FNAMELNAME。在我们的示例中,我们使用 firstNamelastName 以提高可读性。请确保在 MailChimp 中重命名这些合并变量,以便这些示例能正常工作。

// 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');

//If you want to do something else, you can get an instance of the underlying API:
Newsletter::getApi();

Spatie 是一家位于比利时安特卫普的网页设计公司。您可以在我们的网站上找到我们所有开源项目的概述 在这里

安装

您可以使用 composer 通过以下命令安装此包:

composer require spatie/laravel-newsletter

此包将自动注册自己。

要发布配置文件到 config/newsletter.php,请运行:

php artisan vendor:publish --provider="Spatie\Newsletter\NewsletterServiceProvider"

这将发布一个配置文件 newsletter.php 到您的配置目录中,其内容如下:

return [

    /*
     * 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,

];

从 3.x 更新到 4.x

我们的配置文件有一个新名称。我们将名称从 laravel-newsletter.php 更改为 newsletter.php

如果您正在升级到 4.x,包正在寻找新的配置文件名称。在这种情况下,您必须将文件重命名到您的 app\config 文件夹中。

用法

安装此包并填写配置文件中的值后,使用此包将变得轻而易举。以下所有示例都使用外观。不要忘记在文件顶部导入它。

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 的默认合并变量名为 FNAMELNAME。在我们的示例中,我们使用 firstNamelastName 以提高可读性。

您可以使用第三个参数将某人订阅到特定的列表

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');

获取订阅者信息

您可以使用 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();

测试

使用以下命令运行测试:

vendor/bin/phpunit

变更日志

请参阅 CHANGELOG 了解最近更改的详细信息。

贡献

请参阅 CONTRIBUTING 了解详细信息。

安全

如果您发现任何安全相关的问题,请通过电子邮件 freek@spatie.be 联系我们,而不是使用问题跟踪器。

明信片软件

您可以使用此软件包,但如果它进入您的生产环境,我们非常希望您能从家乡给我们寄一张明信片,说明您正在使用我们哪个软件包。

我们的地址是:Spatie,Samberstraat 69D,2060 安特卫普,比利时。

我们将所有收到的明信片 发布在我们的公司网站上

鸣谢

支持我们

Spatie 是一家位于比利时的安特卫普网页设计公司。您可以在我们的网站上找到我们所有开源项目的概述 在这里

如果您的业务依赖于我们的贡献,请通过 Patreon 与我们联系并支持我们。所有承诺都将用于分配人力以维护和开发新功能。

许可

MIT 许可证(MIT)。请参阅 许可文件 了解更多信息。