定制 w4msolutions/w4m-email-gateway 二次开发

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

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

w4msolutions/w4m-email-gateway

Composer 安装命令:

composer require w4msolutions/w4m-email-gateway

包简介

Yii2 mailer adapter that sends emails through the W4M Email Gateway service.

README 文档

README

Yii2 mailer adapter for sending emails through the W4M Email Gateway API.

Package

  • Name: w4msolutions/w4m-email-gateway
  • Type: yii2-extension
  • Namespace: W4MSolutions\W4mEmailGateway

Features

  • Drop-in compatible mailer component for current Yii2 mail usage.
  • Keeps Yii compose/render/layout flow by extending yii\symfonymailer\Mailer.
  • Supports existing yii\symfonymailer\Message calls used across the app.
  • Sends payload to gateway /send endpoint with API key authentication.
  • Converts regular attachments to gateway attachments[] payload.
  • Rewrites inline CID embeds (for example cid:image.jpg) into data: URIs.
  • Retries failed sends with incremental cooldown (default: 3 attempts, waits 1s then 3s).

Class and Function Reference

W4MSolutions\W4mEmailGateway\Mailer

Main Yii mailer component.

  • init() initializes default collaborators (GatewayPayloadBuilder, GatewayClient).
  • sendMessage($message) validates config, maps payload, and executes retry-aware delivery.
  • sendWithRetry(array $payload) runs delivery attempts with incremental cooldown.
  • sendPayload(array $payload) performs one HTTP call through GatewayClient.
  • waitBeforeRetry(int $seconds) sleeps between retries (override-friendly for tests).

W4MSolutions\W4mEmailGateway\GatewayPayloadBuilder

Converts yii\symfonymailer\Message into gateway JSON payload.

  • build(Message $message) maps recipients, subject/body, from/from_name, reply_to, attachments.
  • normalizeAddressList(...) and extractPrimaryAddressAndName(...) standardize address data.
  • buildAttachmentPayload(...) base64-encodes attachment data for gateway transport.

W4MSolutions\W4mEmailGateway\CidEmbedRewriter

Makes inline embedded images compatible with gateway body-only HTML transport.

  • rewrite(string $html, array $attachments) replaces cid: references with data: URIs.

W4MSolutions\W4mEmailGateway\GatewayClient

HTTP transport client.

  • postJson(...) sends request using cURL (preferred) or stream fallback.

Configuration

This is the full configuration array for the mailer component with all options:

'components' => [
    'mailer' => [
        'class' => \W4MSolutions\W4mEmailGateway\Mailer::class,
        'viewPath' => '@common/mail',
        'gatewayUrl' => 'http://mail-gateway.local',
        'gatewayApiKey' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
        'authHeaderName' => 'Authorization', // Optional, default: 'Authorization'
        'authHeaderPrefix' => '', // Optional, default: ''
        'sendPath' => '/send', // Optional, default: '/send'
        'requestTimeout' => 15, // Optional, default: 15 seconds
        'maxRetries' => 3, // Optional, default: 3
        'retryInitialDelaySeconds' => 1, // Optional, default: 1
        'retryDelayIncrementSeconds' => 2, // Optional, default: 2
        'useFileTransport' => true, // default: true
    ],
],

Retry Behavior

Default retry flow is:

  1. Attempt 1 immediately.
  2. If it fails, wait 1 second.
  3. Attempt 2.
  4. If it fails, wait 3 seconds (1 + 2).
  5. Attempt 3.

You can tune this with:

  • maxRetries
  • retryInitialDelaySeconds
  • retryDelayIncrementSeconds

Running Tests (Docker)

Run tests in a dedicated container from this package folder.

Required Tools

  • Docker Engine
  • Docker Compose v2 (docker compose)

All commands below are executed from the package root.

Run All Package Tests

docker compose run --rm tests

Run One Test File (Optional)

docker compose run --rm tests sh docker/run-tests.sh tests/unit/MailerRetryTest.php

Run One Test Method (Optional)

docker compose run --rm tests sh docker/run-tests.sh tests/unit/MailerRetryTest.php --filter testSendMessageRetriesUntilSuccess

Build or Rebuild the Test Image (Optional)

docker compose build

Open a Shell in the Test Container (Optional)

docker compose run --rm tests bash

Notes About the Standalone Setup

  • docker-compose.yml and Dockerfile live inside this package and do not depend on the parent project.
  • docker/run-tests.sh installs Composer dependencies automatically when vendor/autoload.php is missing.
  • A Docker named volume (composer-cache) is used to speed up repeated Composer installs.

Notes

  • The gateway schema documents to, cc, bcc, subject, body, attachments, from_name, and reply_to.
  • This adapter also forwards a from field when available.
  • If useFileTransport is true, Yii saves .eml files and skips gateway delivery.

w4msolutions/w4m-email-gateway 适用场景与选型建议

w4msolutions/w4m-email-gateway 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 69 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 05 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 w4msolutions/w4m-email-gateway 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 69
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 44
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: proprietary
  • 更新时间: 2026-05-14