friendsofhyperf / mail
The Mailer for Hyperf.
Requires
- php: ^8.1
- friendsofhyperf/support: ~3.2.0
- hyperf/collection: ~3.2.0
- hyperf/contract: ~3.2.0
- hyperf/di: ~3.2.0
- hyperf/macroable: ~3.2.0
- hyperf/support: ~3.2.0
- hyperf/view-engine: ~3.2.0
- league/commonmark: ^2.2
- symfony/mailer: ^6.4 || ^7.0
- tijsverkoyen/css-to-inline-styles: ^2.2.5
Suggests
- aws/aws-sdk-php: Required to use the SES mail driver (^3.235.5).
- hyperf/devtool: Required to use the GeneratorCommand (~3.2.0).
- symfony/http-client: Required to use the Symfony API mail transports (^6.2).
- symfony/mailgun-mailer: Required to enable support for the Mailgun mail transport (^6.2).
- symfony/postmark-mailer: Required to enable support for the Postmark mail transport (^6.2).
This package is auto-updated.
Last update: 2026-06-17 01:08:37 UTC
README
The mail component sends view, Markdown, HTML, and plain-text messages through Symfony Mailer transports.
Installation
composer require friendsofhyperf/mail php bin/hyperf.php vendor:publish friendsofhyperf/mail
Publishing the package creates config/autoload/mail.php and copies the mail view components to
storage/views/mail. If your application does not already have the Hyperf view configuration,
publish it separately:
php bin/hyperf.php vendor:publish hyperf/view
The package requires PHP 8.1 or later. Some features require optional packages:
hyperf/devtoolprovides thegen:mailcommand.aws/aws-sdk-phpis required for thesesandses-v2transports.symfony/http-clientis required for Symfony API mail transports.symfony/mailgun-mailerandsymfony/postmark-mailerprovide the corresponding transports.
Configuration
The published configuration defaults to the log mailer. Select a mailer with MAIL_MAILER, then
configure it under mail.mailers. Supported transports are smtp, sendmail, mail, mailgun,
ses, ses-v2, postmark, log, array, failover, and roundrobin. Custom transports can be
registered with Mail::extend().
// config/autoload/mail.php use function Hyperf\Support\env; return [ 'default' => env('MAIL_MAILER', 'log'), 'mailers' => [ 'smtp' => [ 'transport' => 'smtp', 'url' => env('MAIL_URL'), 'host' => env('MAIL_HOST', '127.0.0.1'), 'port' => env('MAIL_PORT', 2525), 'encryption' => env('MAIL_ENCRYPTION', 'tls'), 'username' => env('MAIL_USERNAME'), 'password' => env('MAIL_PASSWORD'), 'timeout' => null, 'local_domain' => env('MAIL_EHLO_DOMAIN'), 'scheme' => env('MAIL_SCHEME', 'smtp'), ], 'log' => [ 'transport' => 'log', 'group' => env('MAIL_LOG_GROUP', 'default'), 'name' => env('MAIL_LOG_NAME', 'mail'), ], ], 'from' => [ 'address' => env('MAIL_FROM_ADDRESS', 'hello@example.com'), 'name' => env('MAIL_FROM_NAME', 'Example'), ], 'markdown' => [ 'theme' => env('MAIL_MARKDOWN_THEME', 'default'), 'paths' => [ BASE_PATH . '/storage/views/mail', ], ], ];
A mailer-specific from, reply_to, to, or return_path value overrides the corresponding
global address. A global to address also removes the original To, Cc, and Bcc recipients before
sending, which is useful in development.
Creating a Mailable
With hyperf/devtool installed, generate a view-based mailable or use --markdown to also create a
Markdown template:
php bin/hyperf.php gen:mail TestMail php bin/hyperf.php gen:mail TestMail --markdown
Envelope defines addresses, subject, tags, metadata, and Symfony message callbacks. Content
accepts view (or its html alias), text, markdown, htmlString, and with. Public properties
declared by your mailable are also exposed to the view.
namespace App\Mail; use FriendsOfHyperf\Mail\Mailable; use FriendsOfHyperf\Mail\Mailable\Attachment; use FriendsOfHyperf\Mail\Mailable\Content; use FriendsOfHyperf\Mail\Mailable\Envelope; class TestMail extends Mailable { public function __construct(public readonly string $name) { } public function envelope(): Envelope { return new Envelope(subject: 'Test Mail'); } public function content(): Content { return new Content( markdown: 'mail.test', with: ['name' => $this->name], ); } public function attachments(): array { return [ Attachment::fromPath(BASE_PATH . '/storage/report.pdf') ->as('report.pdf') ->withMime('application/pdf'), ]; } }
Attachments can also be created with Attachment::fromData(), fromStorage(), or
fromStorageDisk(). A reusable attachable object may implement
FriendsOfHyperf\Mail\Contract\Attachable.
Sending Mail
Mail::mailer() selects a configured mailer; omitting its argument uses mail.default. The
to(), cc(), and bcc() methods return a pending mail object whose send() method accepts a
FriendsOfHyperf\Mail\Contract\Mailable. Sending returns a SentMessage or null when a
MessageSending listener stops delivery.
use App\Mail\TestMail; use FriendsOfHyperf\Mail\Facade\Mail; Mail::mailer('smtp') ->to('user@example.com', 'Example User') ->cc('team@example.com') ->send(new TestMail('Hyperf'));
For messages that do not need a mailable class, the mailer also exposes html(), raw(),
plain(), and send() with a view name or view array. The callback receives a
FriendsOfHyperf\Mail\Message, which forwards unknown methods to the underlying Symfony
Email.
use FriendsOfHyperf\Mail\Facade\Mail; use FriendsOfHyperf\Mail\Message; Mail::html('<h1>Hello</h1>', function (Message $message) { $message->to('user@example.com')->subject('Greeting'); });