定制 gulltour/phpmailer 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

gulltour/phpmailer

Composer 安装命令:

composer require gulltour/phpmailer

包简介

Phpmailer extension for the Yii framework

README 文档

README

Phpmailer extension for the Yii framework yii2-phpmailer

PHPMailer integration for Yii 2 framework

WARNING !!!

Breaking backwards compatibility changes were introduced in Yii2 Framework (see yiisoft/yii2#4071).

If you updated after 26.06.2014 your should fix your application config and calls to mail functions (replace mail with mailer).

In configuration file you should replace:

	'components' => [
        'mail' => [
        ...

with

	'components' => [
        'mailer' => [
        ...

In your code replace:

Yii::$app->mail->... with Yii::$app->mailer->...

(or Yii::$app->getMail()... with Yii::$app->getMailer()... respectively).

===

This extension adds integration of popular PHPMailer library - a "full-featured email creation and transfer class for PHP" - to Yii 2 framework.

Although extension classes implement yii\mail\MailerInterface and yii\mail\MessageInterface, some methods of Yii 2 BaseMailer and BaseMessage are overriden - mainly because of PHPMailer-specific issues.

Nevertheless - the behavior of the extension should remain expected and predictable and comply interfaces mentioned above (I believe so). If not - feel free to report issues.

REQUIREMENTS

You should generally follow Yii 2 requirements. The minimum is that your Web server supports PHP 5.4.0.

INSTALLATION

Install via Composer

If you do not have Composer, you may install it by following the instructions at getcomposer.org.

During the extension installation PHPMailer library will be also installed.

You have two options here - to install latest stable release of PHPMailer (as of now it is 5.2.8 - many significant issues were solved and improvements made in this release) or install latest development version from dev-master.

So, either run

  • for PHPMailer stable:
php composer.phar require --prefer-dist gulltour/phpmailer "~1.0.0"
  • for dev:
php composer.phar require --prefer-dist gulltour/phpmailer "~1.0.0"

or add respectively:

"gulltour/phpmailer": "~1.0.0"

or

"gulltour/phpmailer": "~1.0.0"

to the require section of your composer.json.

Note: this can be affected by 'minimum-stability' settings in your project root composer.json file. If you see error trying to install 'dev-master' branch of PHPMailer you should explicitly point to it, adding

"phpmailer/phpmailer": "*"

to the require section of composer.json in your project root.

Install from an Archive File

You may install extension manually. For that purpose you have to download archive file and extract it into vendor/zyx directory of your project.

You'll also need to download PHPMailer library and extract it to vendor/phpmailer directory of your project.

Note: due to naming agreement 3rd-party extensions and libraries are kept under vendor directory.

One of the benefits of installing via Composer is automation of autoload setup. If you install extension and PHPMailer library from archive files, you'll have to setup autoload paths manually.

Note: currently PHPMailer does not support namespaces (as its minimum requirements is php 5.0), but provides an SPL-compatible autoloader, and...

the preferred way of loading the library - just require '/path/to/PHPMailerAutoload.php'; and everything should work

For further information refer to PHPMailer documentation.

CONFIGURING

To use this extension, you should add some settings in your application configuration file. It may be like the following:

return [
//....
	'components' => [
        'mailer' => [
            'class'            => 'gulltour\phpmailer\Mailer',
            'viewPath'         => '@common/mail',
            'useFileTransport' => false,
            'config'           => [
                'mailer'     => 'smtp',
                'host'       => 'smtp.yandex.ru',
                'port'       => '465',
                'smtpsecure' => 'ssl',
                'smtpauth'   => true,
                'username'   => 'mysmtplogin@example.ru',
                'password'   => 'mYsmTpPassword',
            ],
        ],
	],
];

This is the tipical configuration for sending email via smtp. If you are familiar with PHPMailer, you can see that 'config' array holds settings, similar to corresponding PHPMailer properties. They will be populated when Mailer is initialized.

You can for example define

'mailer' => 'mail' or

'mailer' => 'sendmail', 'sendmail' => '/path/to/sendmail' - just like in PHPMailer.

You may also configure some default message settings for your application in 'messageConfig' array. They will be populated at the moment of message creation.

For example, you may predefine default contents of 'From' email field - so you will not have to set 'From' it every time when composing message:

    ...
    'mailer' => [
        ...
        'messageConfig'    => [
         'from' => ['noreply@example.com' => 'My Example Site']
        ],
        ...
    ],
    ...

USAGE

Example of simple usage:

Yii::$app->mailer->compose()
     ->setFrom(['noreply@example.com' => 'My Example Site'])
     ->setTo([$form->email => $form->name])
     ->setSubject($form->subject)
     ->setTextBody($form->text)
     ->send();

Example of sending html emails rendering view and layout (assumed that default 'From' is set in application configuration file):

Yii::$app->mailer->compose('passwordResetToken', ['user'       => $user,
                                                'title'      => Yii::t('app', 'Password reset'),
                                                'htmlLayout' => 'layouts/html-reset'])
                ->setTo($this->email)
                ->setSubject(Yii::t('app', 'Password reset for ') . Yii::$app->params['siteName'])
                ->send();

Note: possibility to set 'htmlLayout' dynamically when composing message is not the native behavior of BaseMailer. This feature was introduced in this specific extension and is experimental.

For more examples of usage you may refer to documentation on Yii 2 BaseMailer and BaseMessage and PHPDoc blocks in the extension files.

Additionally some Wiki articles are available.

gulltour/phpmailer 适用场景与选型建议

gulltour/phpmailer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 15.5k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 08 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「mail」 「mailer」 「email」 「phpmailer」 「yii2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 gulltour/phpmailer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 gulltour/phpmailer 我们能提供哪些服务?
定制开发 / 二次开发

基于 gulltour/phpmailer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 15.5k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 10
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: BSD
  • 更新时间: 2017-08-22