pluswerk / mail-logger
+Pluswerk TYPO3 扩展:邮件记录器
Requires
- php: ~8.1.0 || ~8.2.0 || ~8.3.0
- composer-runtime-api: ^2
- typo3/cms-core: ^11.5.0 || ^12.4.0
Requires (Dev)
- ext-json: *
- pluswerk/grumphp-config: ^7.1.0
- saschaegerer/phpstan-typo3: ^1.10.1
- spatie/phpunit-snapshot-assertions: ^4.2.17
- ssch/typo3-rector: ^2.6.4
- typo3/testing-framework: ^7.1.0
Replaces
- pluswerk/mail_logger: 4.1.1
- typo3-ter/mail-logger: 4.1.1
This package is auto-updated.
Last update: 2024-08-26 07:08:19 UTC
README
+Pluswerk TYPO3 扩展:邮件记录器
这是一个包含一些邮件功能的 TYPO3 扩展。
扩展安装
通过 composer 安装或直接将文件复制到 TYPO3 扩展文件夹
composer require pluswerk/mail-logger
将 Typoscript 文件添加到您网站的 Typoscript 中
- 在您的常量中添加
@import 'EXT:mail_logger/Configuration/TypoScript/constant.typoscript'
- 并在您的设置中添加
@import 'EXT:mail_logger/Configuration/TypoScript/setup.typoscript'
。
1. 邮件记录
该扩展会自动记录通过 TYPO3 邮件 API 发送的所有 TYPO3 系统邮件。只需安装扩展即可使用。所有发送的邮件都可以在 TYPO3 邮件记录器后端模块中找到。
默认情况下,邮件记录的最大时间为 30 天,可以按以下方式更改: 查看 strtotime 默认情况下,邮件将在 7 天后匿名化。可以通过将 anonymizeAfter 设置为 0 来直接匿名化。
module.tx_maillogger.settings.cleanup { lifetime = 30 days anonymize = 1 anonymizeAfter = 7 days }
2. 邮件模板
您可以为使用 Fluid 编写的 TYPO3 邮件模板进行配置,这些模板可供编辑(在数据库中)编辑,并通过 TypoScript(在 VCS 中)配置。
这是如何工作的? 邮件基本上将在 TypoScript 配置(例如发送地址的配置)中进行配置。之后,编辑将生成一个数据库条目,它通过附加信息(例如 fluid 模板或接收者)扩展此模板。之后可以通过 php 在您的扩展中扩展或覆盖此类电子邮件实例(例如:动态接收者)。
TypoScript 示例
您必须为每封邮件创建一个 TypoScript 模板。 "label" 是唯一必填字段,其他字段为可选。
# E-mail template
module.tx_maillogger.settings.mailTemplates {
exampleMailTemplateKey {
label = This Label will be shown in the Backend for BE-Users, replace this with a good title! :-)
mailFromName = Default Mail-From-Name
mailFromAddress = info@domain.com
mailToNames = Markus Hoelzle, John Doe
mailToAddresses = markus-hoelzle@example.com, john.doe@example.com
mailBlindCopyAddresses = we-read-all-your-mails@example.com
}
}
数据库中的邮件模板
邮件模板将存储在数据库中。只需创建一个 "邮件模板" 记录。在此处选择 TypoScript 模板。消息将通过 Fluid 渲染,因此可以打印变量或使用 ViewHelpers。
示例消息
<f:format.nl2br> Hello, we have {amount} new purchase keys (see attachment). This mail was sent automatically by domain.com </f:format.nl2br>
通过 PHP 发送邮件
电子邮件实例 "\Pluswerk\MailLogger\Domain\Model\Mail\TemplateBasedMailMessage" 继承自 SwiftMailer 类 "\Swift_Message"。因此,电子邮件实例具有以下功能: http://swiftmailer.org/docs/messages.html 最简单的方法是使用 "\Pluswerk\MailLogger\Utility\MailUtility" 类的函数。
基本示例
<?php use \Pluswerk\MailLogger\Utility\MailUtility; MailUtility::getMailByKey('exampleMailTemplateKey', null, ['myVariable' => 'This mail was sent at ' . time(), 'myUser' => $myExtbaseUser])->send();
特定语言的邮件模板
<?php use \Pluswerk\MailLogger\Utility\MailUtility; MailUtility::getMailByKey('exampleMailTemplateKey', 42, ['myVariable' => 'This mail was sent at ' . time(), 'myUser' => $myExtbaseUser])->send();
示例 - 传递电子邮件参数并发送附件(例如 FPDF)
<?php use \Pluswerk\MailLogger\Utility\MailUtility; try { // send mail $mail = MailUtility::getMailByKey('exampleMailTemplateKey', null, [ 'amount' => $amount ]); $pdfFileName = 'myFile.pdf'; $pdfFileByteStream = $fpdf->Output($pdfFileName, 'S'); $pdfFileAttachment = \Swift_Attachment::newInstance($pdfFileByteStream, $pdfFileName, 'application/pdf'); $mail->attach($pdfFileAttachment); $mail->send(); } catch (\Exception $e) { // handle error $this->addFlashMessage('E-Mail could not be sent because of an error: ' . $e->getMessage(), '', AbstractMessage::ERROR); }
您应该在 PHP 代码中捕获异常。经验表明,编辑经常不添加模板(或翻译)等。应该以某种方式处理相应的错误!
自定义 fluid 模板
有时您还希望用您自己的标记包裹数据库中的邮件模板。因此,我们提供了通过 fluid 定制邮件以符合您需求的选项。同样,您可以通过 Typoscript 为每个邮件模板配置渲染定义。
module.tx_maillogger.settings.templateOverrides {
mytemplatekey {
title = My Template
templatePath = EXT:my_ext/Resources/Private/Templates/Mail.html
partialRootPaths = EXT:my_ext/Resources/Private/Partials/
layoutRootPaths = EXT:my_ext/Resources/Private/Layouts/
}
anothertemplatekey {
title = Another Template Key
templatePath = EXT:another_ext/Resources/Private/Templates/Mail.html
settings {
myParameter = myValue
}
}
}
}
<!-- Fluid example --> <h2>{mailTemplate.subject}</h2> <f:format.raw>{message}</f:format.raw> <p>This is my passed value: {settings.myValue}</p>
变量 "message" 和 "mailTemplate" 会自动提供给您的模板。您可以通过使用 "f:format.raw" 视图助手来简单地使用实际消息。您可以为您添加的每个模板提供自己的部分路径和布局路径。否则,它将使用此扩展提供的默认路径。
您可以通过 "settings"-选项向模板添加自己的参数。
示例 - 在您的插件中使用电子邮件模板
如果编辑器可以动态选择邮件模板,您可以在插件中集成 Flexform,添加以下配置
<settings.userMailTemplate> <TCEforms> <label>E-mail template</label> <config> <type>select</type> <renderType>selectSingle</renderType> <foreign_table>tx_maillogger_domain_model_mailtemplate</foreign_table> <foreign_table_where> ORDER BY tx_maillogger_domain_model_mailtemplate.title</foreign_table_where> <size>1</size> <minitems>1</minitems> <maxitems>1</maxitems> </config> </TCEforms> </settings.userMailTemplate>
邮件的 DKIM 签名(目前版本 2.0 不支持)
您可以为用于垃圾邮件保护的每个邮件模板设置 DKIM 签名。因此,您必须定义可以在邮件模板的后端选择的 typoscript 键。
请注意,您必须删除 "-----BEGIN RSA PRIVATE KEY-----" 和 "-----END RSA PRIVATE KEY-----",因为它们是由 PHP 使用特殊字符添加的,您不希望在 typoscript 中输入。因此,仅粘贴您的私钥链作为密钥。
有关使用 DKIM 签名和将 TXT 记录添加到您的 DNS 的示例,您可以访问 这篇文章
密钥:没有 "-----BEGIN RSA PRIVATE KEY-----" 和 "-----END RSA PRIVATE KEY-----" 的您的私钥 域:您想要发送邮件的域名(例如 info@example.com)选择器很可能是 "default"。
module.tx_maillogger.settings.dkim {
examplekey {
key = MYRSAPRIVATEKEY
domain = example.com
selector = default
}
}