PHP code example of goletter / hyperf-mail

1. Go to this page and download the library: Download goletter/hyperf-mail library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

goletter / hyperf-mail example snippets


use App\Mail\OrderShipped;
use Goletter\Mail\Mail;

Mail::to($user)->send(new OrderShipped($order));

'smtp' => [
    'host' => env('MAIL_SMTP_HOST'),
    'port' => (int) env('MAIL_SMTP_PORT', 587),
    'encryption' => env('MAIL_SMTP_ENCRYPTION', 'tls'), // tls | ssl | null
    'username' => env('MAIL_SMTP_USERNAME'),
    'password' => env('MAIL_SMTP_PASSWORD'),
    'dsn' => env('MAIL_SMTP_DSN'), // 可选,设置后优先生效
],

'aliyun_dm' => [
    'transport' => \Goletter\Mail\Transport\AliyunDmTransport::class,
    'options' => [
        'access_key_id' => env('MAIL_ALIYUN_DM_ACCESS_KEY_ID'),
        'access_secret' => env('MAIL_ALIYUN_DM_ACCESS_SECRET'),
        'region_id' => env('MAIL_ALIYUN_DM_REGION_ID'),
        'click_trace' => env('MAIL_ALIYUN_DM_CLICK_TRACE', '0'),
    ],
],

'resend' => [
    'transport' => \Goletter\Mail\Transport\ResendTransport::class,
    'options' => [
        'access_key_id' => env('MAIL_RESEND_ACCESS_KEY_ID'),
    ],
],

'sendmail' => [
    'dsn' => 'sendmail://default',
],

'log' => [
    'transport' => \Goletter\Mail\Transport\LogTransport::class,
    'options' => [
        'name' => 'mail.local',
        'group' => 'default',
    ],
],

'from' => [
    'address' => env('MAIL_FROM_ADDRESS', '[email protected]'),
    'name' => env('MAIL_FROM_NAME', 'Example'),
],

// 可选:全局 reply-to
'reply_to' => [
    'address' => '[email protected]',
    'name' => 'Support',
],

// 可选:本地调试时,所有邮件改发到同一地址
'to' => [
    'address' => '[email protected]',
    'name' => 'Dev',
],



declare(strict_types=1);

namespace App\Mail;

use Goletter\Mail\Mailable;

class OrderShipped extends Mailable
{
    public function __construct(
        public string $userName,
    ) {
    }

    public function build(): void
    {
        $this->subject('订单已发货')
            // HTML 与纯文本可同时设置,会组成 multipart 邮件
            ->htmlBody(sprintf(
                '<h1>Hello, %s</h1><p>您的订单已发货。</p>',
                htmlspecialchars($this->userName, ENT_QUOTES, 'UTF-8')
            ))
            ->textBody(sprintf('Hello, %s — 您的订单已发货。', $this->userName));
    }
}

use App\Mail\OrderShipped;
use Goletter\Mail\Mail;

Mail::to('[email protected]')->send(new OrderShipped('Yong'));

public function build(): void
{
    $this->from('[email protected]', 'App Name')
        ->subject('订单已发货');
}

public function build(): void
{
    $this->subject('通知')
        ->htmlBody('<p>Hello</p>')
        ->textBody('Hello'); // 可选;与 htmlBody 并存时为 multipart,不是附件
}

public function build(): void
{
    $this->subject('订单已发货')
        ->htmlView('emails.orders.shipped')
        ->textView('emails.orders.shipped_plain'); // 可选
}

public function __construct(public Order $order) {}

public function build(): void
{
    $this->htmlView('emails.orders.shipped')
        ->with([
            'orderName' => $this->order->name,
            'orderPrice' => $this->order->price,
        ]);
}

// 发送时指定
Mail::mailer('postmark')->to($user)->send(new OrderShipped($order));

// 或在 Mailable 内指定
public function build(): void
{
    $this->mailer('smtp')
        ->subject('订单已发货')
        ->htmlView('emails.orders.shipped');
}

$this->htmlView('emails.orders.shipped')
    ->attach('/path/to/file', ['as' => 'name.pdf', 'mime' => 'application/pdf'])
    ->attachData($pdfBytes, 'name.pdf', ['mime' => 'application/pdf'])
    ->attachFromDefaultStorage('/path/to/file', 'name.pdf', ['mime' => 'application/pdf'])
    ->attachFromStorage('s3', '/path/to/file', 'name.zip', ['mime' => 'application/zip']);

public function build(): void
{
    $this->htmlView('emails.orders.shipped');

    $this->withEmail(function (\Symfony\Component\Mime\Email $message) {
        $message->getHeaders()->addTextHeader('X-Custom', 'value');
    });
}

use Goletter\Mail\Mail;

Mail::to($request->user())->send(new OrderShipped($order));

Mail::to($user)
    ->cc($moreUsers)
    ->bcc($evenMoreUsers)
    ->send(new OrderShipped($order));

// 指定 mailer(见上方「指定 Mailer」)
Mail::mailer('postmark')
    ->to($user)
    ->send(new OrderShipped($order));

foreach (['[email protected]', '[email protected]'] as $recipient) {
    Mail::to($recipient)->send(new OrderShipped($order));
}

Mail::to($user)->queue(new OrderShipped($order));
Mail::to($user)->queue(new OrderShipped($order), 'emails'); // 指定队列

Mail::to($user)->later(new OrderShipped($order), 300); // 延迟秒数,单位与驱动一致
Mail::to($user)->later(new OrderShipped($order), 300, 'emails');

use HyperfExt\Contract\ShouldQueue;
use Goletter\Mail\Mailable;

class OrderShipped extends Mailable implements ShouldQueue
{
    public string $queue = 'default';
}

$html = Mail::render(new InvoicePaid($invoice));
// 或
$html = (new InvoicePaid($invoice))->render();

Mail::to($user)->locale('es')->send(new OrderShipped($order));
bash
php bin/hyperf.php vendor:publish goletter/hyperf-mail
bash
php bin/hyperf.php gen:mail OrderShipped
bash
composer 
bash
php bin/hyperf.php gen:mail OrderShipped
blade
<div>Price: {{ $order->price }}</div>
blade
<img src="{{ $message->embed($pathToImage) }}">
<img src="{{ $message->embedData($data, $name) }}">