vernes/yiimailer
Composer 安装命令:
composer require vernes/yiimailer
包简介
Yii extension for sending emails with layouts using PHPMailer
README 文档
README
Yii (v1.1.x) extension for sending emails with layouts using PHPMailer
Features
- Based on PHPMailer
- Supports Yii layouts and translation
- Supports web and console applications
- Send full HTML emails with embedded images
- Work with views like you usually do in Yii
- Use test mode to save emails instead of sending them (useful when you don't have mail server installed locally)
Installation
- Install with composer:
composer require vernes/yiimailer - Add 'vendor.vernes.yiimailer.YiiMailer' line to your imports in main and/or console yii config
- Copy mail.php config file to protected/config or add configuration array in 'params' under the key 'YiiMailer'
- Create email layout file mail.php in protected/views/layouts/ (default path, can be changed in config)
- Create all the views you want to use in protected/views/mail/ (default path, can be changed in config)
- Put all images you want to embed in emails in images/mail/ (default path, can be changed in config)
Note
In console apps, alias 'webroot' may not be the same as in web apps. See yiisoft/yii#1267. Best workaround is to set alias you need directly in config/console.php, e.g.:
Yii::setPathOfAlias('webroot.images.mail', '/path/to/your/images/mail/dir');
Yet another solution is to override protected/yiic.php and set 'webroot' alias there (see example files).
Usage
Instantiate YiiMailer in your controller or console command and pass view and data array:
$mail = new YiiMailer('contact', array('message' => 'Message to send', 'name' => 'John Doe', 'description' => 'Contact form'));
or
$mail = new YiiMailer();
$mail->setView('contact');
$mail->setData(array('message' => 'Message to send', 'name' => 'John Doe', 'description' => 'Contact form'));
Layout is automatically set from config but you may override it with
$mail->setLayout('layoutName');
Set the properties:
$mail->setFrom('from@example.com', 'John Doe');
$mail->setTo(Yii::app()->params['adminEmail']);
$mail->setSubject('Mail subject');
You may use all PHPMailer properties you would usually use.
And finally send email(s):
if ($mail->send()) {
Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
} else {
Yii::app()->user->setFlash('error','Error while sending email: '.$mail->getError());
}
Sending simple messages
You can send email without both the layout and view by using:
$mail = new YiiMailer();
//$mail->clearLayout();//if layout is already set in config
$mail->setFrom('from@example.com', 'John Doe');
$mail->setTo(Yii::app()->params['adminEmail']);
$mail->setSubject('Mail subject');
$mail->setBody('Simple message');
$mail->send();
Alternatively, you may also send email message with layout but without specific view (set layout and set body) or with view but without layout (clear layout and set view).
Setting addresses
When using methods for setting addresses (setTo(), setCc(), setBcc(), setReplyTo()) any of the following is valid for arguments:
$mail->setTo('john@example.com');
$mail->setTo(array('john@example.com','jane@example.com'));
$mail->setTo(array('john@example.com'=>'John Doe','jane@example.com'));
Sending attachments
You may send one or more attachments using setAttachemnt() method:
$mail->setAttachment('something.pdf');
$mail->setAttachment(array('something.pdf','something_else.pdf','another.doc'));
$mail->setAttachment(array('something.pdf'=>'Some file','something_else.pdf'=>'Another file'));
Test mode
When working locally without mail server installed, it may be useful to save emails as files instead of trying to send them and getting errors in the process. To use test mode, you must specify path to directory where you want to save your emails and set 'testMode' property to 'true' in your config:
'savePath' => 'webroot.assets.mail', 'testMode' => true,
Emails are saved as .eml files and you can use software like Mozilla Thunderbird to open them.
Using SMTP
If you want to use SMTP, configure appropriate properties in your config. Example setup for GMail:
'Mailer' => 'smtp', 'Host' => 'smtp.gmail.com', 'Port' => 465, 'SMTPSecure' => 'ssl', 'SMTPAuth' => true, 'Username' => 'your_email@gmail.com', 'Password' => 'your_password',
You may also configure this just before sending email:
$mail->setSmtp('smtp.gmail.com', 465, 'ssl', true, 'your_email@gmail.com', 'your_password');
Examples
Two examples included: one for standard contact form in yii web app and the other one for yii console app.
vernes/yiimailer 适用场景与选型建议
vernes/yiimailer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 122.53k 次下载、GitHub Stars 达 81, 最近一次更新时间为 2018 年 02 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 vernes/yiimailer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 vernes/yiimailer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 122.53k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 81
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2018-02-08