reelworx/t3-mailservice

一个库,用于TYPO3扩展方便地通过Fluid模板发送HTML和TXT内容的电子邮件

v2.4.0 2024-02-15 16:16 UTC

This package is auto-updated.

Last update: 2024-09-15 16:46:42 UTC


README

此库可以被TYPO3扩展用于方便地发送电子邮件。

功能

  • 基于Fluid的HTML和TEXT格式的邮件模板
  • 多语言(包括备用语言)
  • 适用于所有环境(前端、后端、CLI)
  • 通过ViewHelpers嵌入图片
  • 渲染后事件以进一步修改邮件内容

邮件模板要求

每封邮件至少需要两个模板文件中的一个

  • HTML模板:存储在 <language>/<ControllerName>/<mailName>.html
  • TEXT模板:存储在 <language>/<ControllerName>/<mailName>.txt

其中

  • <language> 是在网站配置中设置的ISO-2语言代码(小写)
  • <ControllerName> 是当前控制器名称,使用 $mailConfig->controllerName 配置(见下文)
  • <mailName> 是电子邮件的自由名称

两个文件都是Fluid模板!(在txt格式中不执行HTML转义)两个文件都需要使用UNIX行结束符。

示例模板可以在本库的 template-examples 文件夹中找到。

邮件主题

模板的 第一行 包含邮件的 主题。如果使用两个模板,则一个模板中的主题行可能为空。

在您的扩展中使用

function sendMail()
{ 
    // optional in non-frontend environment
    // if your mail needs to generate links to a frontend site, you need to fake a FE environment with:
    // \Reelworx\TYPO3\FakeFrontend\FrontendUtility::buildFakeFE(<uid of some frontend page>);

    // assuming TypoScript settings holds all config  (see reference below)
    $mailConfig = MailConfiguration::fromArray($this->settings['mail']);
    
    $mailConfig->extensionName = 'MyExtension';
    $mailConfig->controllerName = 'Controller';
    $mailConfig->pluginName = 'MyPlugin';
    
    $mailService = GeneralUtility::makeInstance(MailService::class, $mailConfig);
    $msg = $mailService->createMessage();

    // within extbase controllers:
    $mailView = $mailService->createMailView($msg, $this->configurationManager->getContentObject(), $this->controllerContext);
    // otherwise:
    $mailView = $mailService->createMailView($msg);
    
    $mailView->assign('mydata', 'somedata');
    
    $msg->setContent($mailService->renderMail($mailView, '<mailName>'));
    $msg->setTo($someRecipient);
    
    // optional:
    $msg->attachFromPath($someFalFile->getForLocalProcessing(false));
    
    $msg->send();
}

Extbase控制器

在Extbase控制器中,通过使用提供的特质,简化了设置。

重要假设:邮件配置可以在 $this->settings['mail'] 中找到

class FooController extends ActionController
{
   use \Reelworx\TYPO3\MailService\ExtbaseMailTrait;

   /* ... */
   public function myAction(): void
   {
       // optional in non-frontend environment
       // if your mail needs to generate links to a frontend site, you need to fake a FE environment with:
       // \Reelworx\TYPO3\FakeFrontend\FrontendUtility::buildFakeFE(<uid of some frontend page>);
   
       //
       // Assumption: TypoScript settings 'mail' holds all mail config (see reference below)
       //
       
       /** @var MailService $mailService */
       /** @var MailMessage $msg */
       /** @var StandaloneView $mailView */
       $this->createMailMessage($mailService, $mailView, $msg);
       
       $mailView->assign('mydata', 'somedata');
       
       $msg->setContent($mailService->renderMail($mailView, '<mailName>'));
       
       $msg->setTo($someRecipient);
       
       // optional:
       $msg->attachFromPath($someFalFile->getForLocalProcessing(false));

       $msg->send();
   }
}

渲染后事件以更改邮件内容

您可以可选地为 MailService 配置Core的EventDispatcher,这需要手动注入。(ExtbaseMailTrait 已为您处理了此问题。)

这允许您使用 MailRenderedEvent

注册您的 MailRenderedEvent 事件处理器(参见 TYPO3 文档

事件具有获取器,可以检索 MailContent 对象,您可以修改以满足您的需求。

示例用法:使用一些CSS内联或其他处理工具修改HTML内容,以获得出色的邮件布局质量。例如,检查 MJML

配置参考

如果您通过TypoScript配置邮件设置(您应该这样做,以便使用 ExtbaseMailTrait),则设置应如下所示

plugin.tx_yourext.settings.mail {
    view {
        templateRootPaths.10 = EXT:yourext/Resources/Private/Mail/Templates/
        layoutRootPaths.10 = EXT:yourext/Resources/Private/Mail/Layouts/
        partialRootPaths.10 = EXT:yourext/Resources/Private/Mail/Partials/
    }
    
    // the following settings are optional
    sender_name = company website
    sender_email = noreply@example.com
    recipient_copy = bcc@example.com
    replyTo = office@example.com
    // this will be the Organization header of messages
    organization = The Company

    // optional: Define allowed languages; The first listed language is used as fallback
    // e.g. Always send mails in EN no matter which language the website run with
    languages = en,de
}