ddrv/mailer
Composer 安装命令:
composer require ddrv/mailer
包简介
PHP library for sending email
README 文档
README
Mailer
PHP library for sending email.
Install
-
Run in console:
composer require ddrv/mailer:^5
-
Include autoload file
require_once('vendor/autoload.php');
Usage
Creating transport instance
Sendmail
<?php $transport = new Ddrv\Mailer\Transport\SendmailTransport( '-f' // sendmail options );
SMTP
<?php $transport = new Ddrv\Mailer\Transport\SmtpTransport( 'smtp.fight.club', // host (REQUIRED) 25, // port (REQUIRED) 'info', // login (REQUIRED) 'super-secret', // password (REQUIRED) 'joe@fight.club', // sender email (REQUIRED) null, // encryption: 'tls', 'ssl' or null 'http://fight.club' // domain );
Fake (emulation send emails)
<?php $transport = new Ddrv\Mailer\Transport\FakeTransport();
Custom
You can implement Ddrv\Mailer\Contract\Transport interface
<?php /** @var Ddrv\Mailer\Contract\Transport $transport */
Creating mailer instance
<?php /** @var Ddrv\Mailer\Contract\Transport $transport */ $mailer = new Ddrv\Mailer\Mailer($transport);
Creating message instance
<?php $message = new Ddrv\Mailer\Message( 'Subject', // Subject '<p>HTML</p>', // HTML body 'Simple text.' // Text plain body );
Sending message
<?php /** * @var Ddrv\Mailer\Contract\Message $message * @var Ddrv\Mailer\Mailer $mailer */ $mailer->send($message);
Message methods
<?php /** @var Ddrv\Mailer\Message $message */ $message->setSubject('Welcome to Fight Club!'); $message->setText('Welcome to Fight Club!' . PHP_EOL . 'Please, read our rules in attachments.'); $html = '<h1>Welcome to Fight Club!</h1><p>Please, read our rules in attachments.</p>'; $html .= '<img src="cid:poster" alt="Poster"/><img src="cid:ticket" alt="Your ticket"/>'; $message->setHtml($html); $message->setSender('support@fight.club', 'Support od Fight Club'); // The SMTP transport will set its value. $message->addRecipient('tyler@fight.club', 'Tyler Durden', Ddrv\Mailer\Message::RECIPIENT_TO); $message->addRecipient('bob@fight.club', 'Robert Paulson', Ddrv\Mailer\Message::RECIPIENT_CC); $message->addRecipient('angel@fight.club', 'Angel Face', Ddrv\Mailer\Message::RECIPIENT_BCC); $message->addRecipient('r.chesler@car-vendor.com', 'Richard Chesler', Ddrv\Mailer\Message::RECIPIENT_TO); $message->getRecipientName('bob@fight.club'); // Returns 'Robert Paulson'. $message->getRecipientName('unknown@fight.club'); // Returns null. $message->removeRecipient('r.chesler@car-vendor.com'); // If you change your mind. // You may remove recipients by type $message->removeRecipients(Ddrv\Mailer\Message::RECIPIENT_TO); $message->removeRecipients(Ddrv\Mailer\Message::RECIPIENT_CC); $message->removeRecipients(Ddrv\Mailer\Message::RECIPIENT_BCC); // Or all $message->removeRecipients(); $message->addRecipient('tyler@fight.club', 'Tyler Durden', Ddrv\Mailer\Message::RECIPIENT_TO); $message->addRecipient('bob@fight.club', 'Robert Paulson', Ddrv\Mailer\Message::RECIPIENT_CC); $message->addRecipient('angel@fight.club', 'Angel Face', Ddrv\Mailer\Message::RECIPIENT_BCC); $rules = <<<TEXT 1. You don't talk about fight club. 2. You don't talk about fight club. 3. When someone says stop, or goes limp, the fight is over. 4. Only two guys to a fight. 5. One fight at a time. 6. They fight without shirts or shoes. 7. The fights go on as long as they have to. 8. If this is your first night at fight club, you have to fight. TEXT; $message->attachFromString( 'rules.txt', // Attachment name (REQUIRED) $rules, // Contents (REQUIRED) 'text/plain; charset=UTF-8' // Mime Type ); $path = '/home/tyler/docs/projects/mayhem/rules.txt'; $message->attachFromFile( 'project-mayhem.txt', // Attachment name (REQUIRED) $path, // Path to attached file (REQUIRED) 'text/plain; charset=UTF-8' // Mime Type ); $message->detach('project-mayhem.txt'); // If you change your mind. $message->setHtmlContentFromString( 'ticket', // HTML Content ID file_get_contents('/home/tyler/tickets/038994.jpg'), // Contents (REQUIRED) 'image/jpeg' // Mime Type ); $message->setHtmlContentFromString( 'script', // HTML Content ID 'alert("ok");', // Contents (REQUIRED) 'application/javascript' // Mime Type ); $message->setHtmlContentFromFile( 'poster', // HTML Content ID '/home/tyler/images/fight-club.jpg', // Path to file (REQUIRED) 'image/jpeg' // Mime Type ); $message->unsetBodyHtmlContent('script'); // If you change your mind. $message->hasHeader('X-Some-Header'); // Returns false. $message->setHeader('X-Some-Header', 'Value'); // Header set. $message->hasHeader('X-Some-Header'); // Returns true. $message->getHeader('X-Some-Header'); // Returns 'Value'. $message->removeHeader('X-Some-Header'); // Header removed. $message->hasHeader('X-Some-Header'); // Returns false. $message->getRecipients(); // Returns array if recipients emails. $message->getSubject(); // Returns mail subject. $message->getHeadersRaw(); // Returns string of mail headers. $message->getBodyRaw(); // Returns string of mail body.
You can implement Ddrv\Mailer\Contract\Message interface for work with custom messages.
Spool
This package allows you to use mail spool.
This requires:
- create a spool object (instance of
Ddrv\Mailer\Contract\Spool) - create a transport object (instance of
Ddrv\Mailer\Contract\Transport) - wrap them up in special transport
Ddrv\Mailer\Transport\SpoolTransport
<?php /** * @var Ddrv\Mailer\Contract\Transport $transport * @var Ddrv\Mailer\Contract\Message $message1 * @var Ddrv\Mailer\Contract\Message $message2 */ $spool = new Ddrv\Mailer\Spool\MemorySpool(); // or $spool = new Ddrv\Mailer\Spool\FileSpool('/path/to/emails'); // Or any implementation of Ddrv\Mailer\Contract\Spool $wrapper = new Ddrv\Mailer\Transport\SpoolTransport($transport, $spool); $mailer = new Ddrv\Mailer\Mailer($wrapper); $mailer->send($message1); $mailer->send($message2); // Now the letters will only be added to the queue. // To send them, you need to execute: $wrapper->flush( 100, // Number of emails sent. 5 // Number of attempts to send emails. );
Personal mailing
If you have a copy of a message with many recipients, but you want to send separate emails to each recipient (no copies)
<?php /** * @var Ddrv\Mailer\Mailer $mailer * @var Ddrv\Mailer\Contract\Message $message */ $mailer->personal($message);
Transport factory
If you use native library transport, you can use Ddrv\Mailer\TransportFactory.
<?php use Ddrv\Mailer\TransportFactory; // smtp $transport = TransportFactory::make('smtp://user:password@example.com:465/?encryption=tls&domain=example.com&sender=user%40exapmle.com&name=Informer'); // sendmail $transport = TransportFactory::make('sendmail://localhost/?options=-i+-r+user%40example.com'); // file $transport = TransportFactory::make('file:////path/to/mail/files'); // fake $transport = TransportFactory::make('fake://localhost');
ddrv/mailer 适用场景与选型建议
ddrv/mailer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 79 次下载、GitHub Stars 达 2, 最近一次更新时间为 2017 年 08 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「mail」 「email」 「smtp」 「send」 「sendmail」 「attachments」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ddrv/mailer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ddrv/mailer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ddrv/mailer 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Courier offers a convenient and painless solution for creating emails tailored for your Kirby website.
Mail libraries used by Zimbra Api
Extensible library for building notifications and sending them via different delivery channels.
The Message Submission Agent Diagnostics tool (msadiag) facilitates testing the compatibility of third party message submission agents.
Email+ extends Kirby's email capabilities by adding support for multiple email services using the same Kirby email API.
PHPMailer helper class for sending emails using SMTP
统计信息
- 总下载量: 79
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-08-04