定制 lekoala/silverstripe-sparkpost 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

lekoala/silverstripe-sparkpost

Composer 安装命令:

composer require lekoala/silverstripe-sparkpost

包简介

SparkPost integration for SilverStripe CMS

README 文档

README

Build Status Scrutinizer Code Quality Code Coverage Build Status codecov.io

Latest Stable Version Latest Unstable Version Total Downloads License Monthly Downloads Daily Downloads

Setup

Define in your .env file the following variable

SPARKPOST_API_KEY='YOUR_API_KEY_HERE'

or by defining the api key in your config.yml

LeKoala\SparkPost\SparkPostHelper:
    api_key: "YOUR_API_KEY_HERE"

This module uses a custom client (not the official PHP SDK).

You can also autoconfigure the module with the following environment variables

# Will log emails in the temp folders
SPARKPOST_ENABLE_LOGGING=true
# Will disable sending (useful in development)
SPARKPOST_SENDING_DISABLED=true

By defining the Api Key, the module will register a new transport that will be used to send all emails.

If you're using the SparkPost EU service you can change the API endpoint

# Will use https://api.eu.sparkpost.com/api/v1
SPARKPOST_EU=true

Register the new mailer

If you define the SPARKPOST_API_KEY variable, the mailer transport will be automatically registered.

Otherwise, you need to call the following line:

SparkPostHelper::registerTransport();

Admin sender

By default in SilverStripe, emails without a from email will use the Email::admin_email value.

This is not convenient for websites using a value taken from the SiteConfig, as resolved with SparkPostHelper::resolveDefaultFromEmail.

The SparkPostApiTransport can automatically take care of that and replace any admin email with the set value using the following config flag:

LeKoala\SparkPost\SparkPostHelper:
    override_admin_email: true

Make sure to set this after having processed the sparkpost config.

Check if emails can be used as sender

As a convenience, this library offers an utility SparkPostHelper::isEmailDomainReady to help you determine if an email is ready to be used as a sender.

Please note that this function makes an api call so you may not want to use this to often. It is a better to use this on verification screens or as part of a validation workflow.

Subaccounts support

If you use a master api key, but need to limit data access, you can configure a subaccount id

SPARKPOST_SUBACCOUNT_ID=1234;

or through the YML config.

SparkPost integration

This module create a new admin section that allows you to:

  • List all messages events and allow searching them
  • Have a settings tab to list and configure sending domains and webhook

NOTE : Make sure that you have a valid api key (not a subaccount key) to access features related to installation of the webhook through the CMS.

Note that by default the messages are cached (or not) according to config. You can disable this with the following env key

SPARKPOST_DISABLE_CACHE=true

Setting tags or metadata

By using custom headers you can pass parameters to the api by following the same principle than the SMTP api.

The main way to pass parameters is to add a json encoded string through the X-MSYS-API header, but you can also use that Mandrill compatiblity layer.

For full details, look at the documentation

$email = new Email();
$email->setSubject($sellerTitle . ' - Invoice - ' . $date);
$email->setBody($body);
// Through Mandrill compat layer
$email->getHeaders()->addTextHeader('X-MC-Metadata', json_encode(['RecordID' => $this->ID]));
// Or use M-SYS header
$email->getHeaders()->addTextHeader('X-MSYS-API', json_encode(['metadata' => ['RecordID' => $this->ID]]));

Webhooks

From the SparkPost Admin, you can setup a webhook for your website. This webhook will be called and SparkPostController will take care of handling all events for you. It is registered under the __sparkpost/ route.

By default, SparkPostController will do nothing. Feel free to add your own extensions to SparkPostController to define your own rules, like "Send an email to the admin when a receive a spam complaint".

SparkPostController provides the following extension point for all events:

  • onAnyEvent

And the following extensions points depending on the type of the event:

  • onEngagementEvent
  • onGenerationEvent
  • onMessageEvent
  • onUnsubscribeEvent

You can also inspect the whole payload and the batch id with

  • beforeProcessPayload : to check if a payload has been processed
  • afterProcessPayload : to mark the payload has been processed or log information

You can test if your extension is working properly by visiting /__sparkpost/test if your site is in dev mode. It will load sample data from the API.

Please ensure that the url for the webhook is properly configured if required by using the following configuration

LeKoala\SparkPost\SparkPostAdmin:
    webhook_base_url: "https://my.domain.com/"

You can also define the following environment variable to log all incoming payload into a given directory. Make sure the directory exists. It is relative to your base folder.

SPARKPOST_WEBHOOK_LOG_DIR='_incoming'

Please also pay attention to the fact that the webhook is called for ALL events of your SparkPost account, regardless of the fact of which API key generated the transmission.

To help you overcome this, if a subaccount id is defined, events will be filtered according to this subaccount.

Preventing spam

Make sure you have properly configured your SPF and DKIM records for your domain.

mydomain.com   TXT   "v=spf1 include:myauthorizeddomain.com include:sparkpostmail.com ~all”

Create a DMARC record. See why this is important.

_dmarc.mydomain.com. 3600 IN TXT "v=DMARC1; p=quarantine; sp=quarantine; rf=afrf; pct=100; ri=86400;"

Leave provide_plain option to true or provide plain content for your emails

Use Mail Tester to troubleshoot your issues

Inlining styles

Although SparkPost can inline styles for you, it may not work properly for complex style sheet, such as Foundation Emails. This is why the package pelago\emogrifier is not required by default and styles are inlined in php to get the best results.

If you want to restore built-in functionnality, use this:

LeKoala\SparkPost\SparkPostHelper:
    inline_styles: false
    default_params:
    inlineCss: true

Migration from Swift Mailer

SilverStripe 5 replaced swift mailer by symfony/mailer

Make sure to read the docs https://docs.silverstripe.org/en/5/developer_guides/email/ https://symfony.com/doc/current/mailer.html

Compatibility

Tested with SilverStripe 5+

For 4.9+ compatibility, use branch 3

For 4.x compatibility, use branch 2

For 3.x compatibility, use branch 1

Maintainer

LeKoala - thomas@lekoala.be

lekoala/silverstripe-sparkpost 适用场景与选型建议

lekoala/silverstripe-sparkpost 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11.04k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2016 年 04 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「module」 「cms」 「silverstripe」 「sparkpost」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 lekoala/silverstripe-sparkpost 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 lekoala/silverstripe-sparkpost 我们能提供哪些服务?
定制开发 / 二次开发

基于 lekoala/silverstripe-sparkpost 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 11.04k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 1

GitHub 信息

  • Stars: 7
  • Watchers: 2
  • Forks: 11
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-04-16