定制 julienramel/cloudflare-mailer 二次开发

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

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

julienramel/cloudflare-mailer

Composer 安装命令:

composer require julienramel/cloudflare-mailer

包简介

Symfony Mailer bridge for Cloudflare Email Service

README 文档

README

Tests License: MIT

Symfony Mailer bridge for Cloudflare Email Service.

Note: Cloudflare Email Sending is currently in public beta. The API may evolve before general availability. Pin your dependency to a specific version of this package.

Requirements

Installation

composer require julienramel/cloudflare-mailer

Configuration

DSN

MAILER_DSN=cloudflare+api://ACCOUNT_ID:API_TOKEN@default
Part Description
ACCOUNT_ID Your Cloudflare Account ID
API_TOKEN A Cloudflare API token with Email Sending permission

Symfony Mailer (config/packages/mailer.yaml)

framework:
    mailer:
        dsn: '%env(MAILER_DSN)%'

Register the transport factory (config/services.yaml)

services:
    JulienRamel\CloudflareMailer\Transport\CloudflareTransportFactory:
        tags:
            - { name: mailer.transport_factory }

Usage

use Symfony\Component\Mime\Email;
use Symfony\Component\Mailer\MailerInterface;

class MyService
{
    public function __construct(private readonly MailerInterface $mailer) {}

    public function sendWelcome(string $to): void
    {
        $email = (new Email())
            ->from(new Address('noreply@yourdomain.com', 'My App'))
            ->to($to)
            ->subject('Welcome!')
            ->text('Thanks for signing up.')
            ->html('<h1>Thanks for signing up.</h1>');

        $this->mailer->send($email);
    }
}

Handling bounces

Unlike most email providers (which report bounces asynchronously via webhooks), Cloudflare includes permanent bounce information directly in the API response. This bridge surfaces it via a Symfony event so your application can react immediately.

How it works

send() called
    │
    ├── HTTP error / API failure  →  HttpTransportException thrown
    │
    └── HTTP 200 + success: true
            │
            ├── no permanent bounces  →  SentMessage returned, nothing else
            │
            └── permanent bounces present  →  CloudflareBounceEvent dispatched
                                               SentMessage returned (always)

A bounce — even a total one — is a business failure, not a transport failure. The API call itself succeeded. No exception is thrown; your listener decides what to do.

Registering the listener

use JulienRamel\CloudflareMailer\Event\CloudflareBounceEvent;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;

#[AsEventListener]
final class CloudflareBounceListener
{
    public function __construct(
        private readonly ContactRepository $contacts,
    ) {}

    public function __invoke(CloudflareBounceEvent $event): void
    {
        foreach ($event->getBouncedAddresses() as $address) {
            // The address does not exist or is permanently unreachable.
            // Common reactions: mark as invalid, remove from mailing list,
            // alert your ops team, increment a counter, etc.
            $this->contacts->markAsUndeliverable($address);
        }
    }
}

Treating a total bounce as a fatal error

If your use case requires an exception when nobody received the email, throw it yourself inside the listener:

use JulienRamel\CloudflareMailer\Event\CloudflareBounceEvent;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\Mailer\Exception\TransportException;

#[AsEventListener]
final class StrictBounceListener
{
    public function __invoke(CloudflareBounceEvent $event): void
    {
        $result = $event->getSentMessage()->getDebug();
        // Check if anything was delivered by inspecting the debug output,
        // or keep track of delivered/bounced counts in your own logic.

        // Throw to propagate the failure up the call stack:
        throw new TransportException(\sprintf(
            'Email permanently bounced for: %s',
            implode(', ', $event->getBouncedAddresses()),
        ));
    }
}

Inspecting results without a listener

SentMessage::getDebug() always contains a human-readable summary visible in the Symfony web profiler:

Cloudflare Email result:
  Delivered: alice@example.com
  Queued:    none
  Permanent bounces: ghost@nonexistent.tld

Supported features

Feature Supported
Plain text body
HTML body
CC / BCC
Reply-To
Attachments
Inline images (cid:)
Custom headers
Bounce detection ✅ (synchronous, via event)
SMTP ❌ (API only)

Known limitations

Recipient display names are not supported. The Cloudflare REST API only accepts plain email addresses for to, cc, and bcc fields. If you set new Address('john@example.com', 'John Doe') as a recipient, only john@example.com will be sent — the display name John Doe will not appear in the delivered email's To header.

The sender (from) supports display names via the {"address": "...", "name": "..."} format.

Maximum 50 recipients combined across to, cc, and bcc. An InvalidArgumentException is thrown before the API call if this limit is exceeded.

Single Reply-To address. If multiple reply-to addresses are set, only the first one is sent.

Domain setup

Before sending, your domain must be onboarded in Cloudflare Email Sending. Cloudflare will add the necessary DNS records (MX, SPF, DKIM, DMARC) automatically. See the official documentation.

Development

composer install
vendor/bin/phpunit

License

MIT — see LICENSE.

julienramel/cloudflare-mailer 适用场景与选型建议

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

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

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

围绕 julienramel/cloudflare-mailer 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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