dotkernel/dot-mail
Composer 安装命令:
composer require dotkernel/dot-mail
包简介
Dotkernel mail component based on symfony mailer
README 文档
README
Dotkernel's mail service.
dot-mail is a wrapper on top of symfony mailer
Documentation
Documentation is available at: https://docs.dotkernel.org/dot-mail/.
Version History
| Branch | Release | PSR-11 | Mailer | OSS Lifecycle | PHP Version |
|---|---|---|---|---|---|
| 5.2 | >=5.2.0 |
1 || 2 | symfony/mailer |
||
| 5.1 | <5.2.0 |
1 | symfony/mailer |
||
| 5.0 | <5.1.0 |
1 || 2 | symfony/mailer |
||
| 4.0 | <=4.3.0 |
1 | laminas-mail |
Badges
Installation
Install dotkernel/dot-mail by executing the following Composer command:
composer require dotkernel/dot-mail
Configuration
Mail - Sendmail
If your server has Sendmail installed, update the config/autoload/mail.local.php.dist file by setting the transport key like below
<?php return [ 'dot_mail' => [ 'default' => [ //... 'transport' => 'sendmail', //... ] ] ]
Mail - ESMTP
If you want your application to send mails on e.g., registration, contact, then edit the file config/autoload/mail.local.php.
Set the transport, message_options and smtp_options keys like below.
Under message_options key:
from- email address from whom users will receive emails
Under smtp_options key:
host- the mail server's hostname or IP addressport- the mail server's portconnection_config- fill in theusernameandpasswordkeys with the login details of the email used infromabove- if you want to disable auto_tls set
tlskey to false
Note: all other keys can be left as is.
<?php return [ 'dot_mail' => [ 'default' => [ //... 'transport' => 'esmtp' 'message_options' => [ 'from' => '', //... ], 'smtp_options' => [ 'host' => '', 'port' => 25, 'connection_config' => [ 'username' => '', 'password' => '', 'tls' => null, ] ] //... ] ] ]
In config/autoload/local.php add under contact => message_receivers => to key string values with the emails that should receive contact messages
Please add at least one email address in order for a contact message to reach someone
Also feel free to add as many cc as you want under contact => message_receivers => cc key
Sending an e-mail
Below is an example of how to use the email in the most basic way. You can add your own code to it, e.g., to get the user data from a User object or from a config file, to use a template for the body.
Note that addTo is only one of the methods available for the Message class returned by getMessage().
Other useful methods that were not included in the example are addCc(), addBcc(), addReplyTo().
The returned type is boolean, but if the isValid() method is removed, the returned type becomes MailResult which allows the use of getMessage() for a more detailed error message.
See the Testing if an e-mail message is valid section below.
public function sendBasicMail() { $this->mailService->setBody('Email body'); $this->mailService->setSubject('Email subject'); $this->mailService->getMessage()->addTo('email@example.com', 'User name'); $this->mailService->getMessage()->setEncoding('utf-8'); return $this->mailService->send()->isValid(); }
It's optional but recommended to call the above function in a try-catch block to display helpful error messages.
The next example calls the sendBasicMail function from within UserController, but you can implement it in other controllers, just make sure that the controller's construct also includes the FlashMessenger parameter $messenger.
try { $this->userService->sendBasicMail(); $this->messenger->addSuccess('The mail was sent successfully', 'user-login'); //more code... } catch (Exception $exception) { $this->messenger->addError($exception->getMessage(), 'user-login'); //more code... }
Testing if an e-mail message is valid
After sending an e-mail, you can check if the message was valid or not.
The $this->mailService->send()->isValid() method call will return a boolean value.
If the returned result is true, the e-mail was valid, otherwise the e-mail was invalid.
In case your e-mail was invalid, you can check for any errors using $this->mailService->send()->getMessage().
Using the below logic will let you determine if a message was valid or not and log it. You can implement your own custom error logging logic.
$result = $this->mailService->send(); if (! $result->isValid()) { //log the error error_log($result->getMessage()); }
Invalid e-mail messages will not be sent.
Logging outgoing emails
Optionally, you can keep a log of each successfully sent email. This might be useful when you need to know if/when a specific email has been sent out to a recipient.
Logs are stored in the following format:
[YYYY-MM-DD HH:MM:SS]: {"subject":"Test subject","to":["Test Account <test@dotkernel.com>"],"cc":[],"bcc":[]}.
To enable it, make sure that your config/autoload/mail.local.php has the below log configuration under the dot_mail key:
<?php return [ 'dot_mail' => [ ... 'log' => [ 'sent' => getcwd() . '/log/mail/sent.log' ] ] ];
To disable it, set the value of sent to null.
dotkernel/dot-mail 适用场景与选型建议
dotkernel/dot-mail 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 46.44k 次下载、GitHub Stars 达 11, 最近一次更新时间为 2016 年 10 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「mail」 「mailer」 「symfony」 「event」 「services」 「laminas」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dotkernel/dot-mail 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dotkernel/dot-mail 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dotkernel/dot-mail 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The bundle for easy using json-rpc api on your project
Courier offers a convenient and painless solution for creating emails tailored for your Kirby website.
Creates new sare mail transport for laravel applications
Bundle Symfony DaplosBundle
Mail libraries used by Zimbra Api
Email+ extends Kirby's email capabilities by adding support for multiple email services using the same Kirby email API.
统计信息
- 总下载量: 46.44k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 11
- 点击次数: 22
- 依赖项目数: 5
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-10-13