PHP code example of itjackjw / hyperf-mail

1. Go to this page and download the library: Download itjackjw/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/ */

    

itjackjw / hyperf-mail example snippets


[
    // ...
    'mailgun' => [
        'transport' => \HyperfExt\Mail\Transport\MailgunTransport::class,
        'options' => [
            'domain' => env('MAIL_MAILGUN_DOMAIN'),
            'key' => env('MAIL_MAILGUN_KEY'),
            // 如果你不使用此「US」区域, 你可以定义自己的区域终端地址:
            // https://documentation.mailgun.com/en/latest/api-intro.html#mailgun-regions
            'endpoint' => env('MAIL_MAILGUN_ENDPOINT', 'api.mailgun.net'),
        ],
    ],
    // ...
];

[
    // ...
    'postmark' => [
        'transport' => \HyperfExt\Mail\Transport\PostmarkTransport::class,
        'options' => [
            'token' => env('MAIL_POSTMARK_TOKEN'),
        ],
    ],
    // ...
];

[
    // ...
    'aws_ses' => [
        'transport' => \HyperfExt\Mail\Transport\AwsSesTransport::class,
        'options' => [
            'credentials' => [
                'key' => env('MAIL_AWS_SES_ACCESS_KEY_ID'),
                'secret' => env('MAIL_AWS_SES_SECRET_ACCESS_KEY'),
            ],
            'region' => env('MAIL_AWS_SES_REGION'),
        ],
    ],
    // ...
];

[
    // ...
    'aws_ses' => [
        'transport' => \HyperfExt\Mail\Transport\AwsSesTransport::class,
        'options' => [
            'credentials' => [
                'key' => env('MAIL_AWS_SES_ACCESS_KEY_ID'),
                'secret' => env('MAIL_AWS_SES_SECRET_ACCESS_KEY'),
            ],
            'region' => env('MAIL_AWS_SES_REGION'),
            // 附加选项
            'options' => [
                'ConfigurationSetName' => 'MyConfigurationSet',
                'Tags' => [
                    [
                        'Name' => 'foo',
                        'Value' => 'bar',
                    ],
                ],
            ],
        ],
    ],
    // ...
];

[
    // ...
    'aliyun_dm' => [
        'transport' => \HyperfExt\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'),
        ],
    ],
    // ...
];

/**
 * 编译消息。
 */
public function build(): void
{
    $this->from('[email protected]');
}

/**
 * 构建邮件消息。
 *
 * @return $this
 */
public function build()
{
    return $this
        // HTML 模板
        ->htmlView('emails.orders.shipped')
        // 纯文本模板
        ->textView('emails.orders.shipped_plain');
}



namespace App\Mail;

use App\Models\Order;
use HyperfExt\Mail\Mailable;

class OrderShipped extends Mailable
{
    /**
     * 订单实例。
     *
     * @var Order
     */
    public $order;

    /**
     * 创建一个消息实例。
     *
     * @param  \App\Models\Order  $order
     * @return void
     */
    public function __construct(Order $order)
    {
        $this->order = $order;
    }

    /**
     * 构造消息。
     *
     * @return $this
     */
    public function build()
    {
        return $this->htmlView('emails.orders.shipped');
    }
}



namespace App\Mail;

use App\Model\Order;
use HyperfExt\Mail\Mailable;

class OrderShipped extends Mailable
{
    /**
     * 订单实例。
     *
     * @var \App\Models\Order
     */
    protected $order;

    /**
     * 创建一个消息实例。
     *
     * @param  \App\Models\Order $order
     * @return void
     */
    public function __construct(Order $order)
    {
        $this->order = $order;
    }

    /**
     * 构造消息。
     *
     * @return $this
     */
    public function build()
    {
        return $this->htmlView('emails.orders.shipped')
                    ->with([
                        'orderName' => $this->order->name,
                        'orderPrice' => $this->order->price,
                    ]);
    }
}

/**
 * 构建消息
 *
 * @return $this
 */
public function build()
{
    return $this->htmlView('emails.orders.shipped')
                ->attach('/path/to/file');
}

/**
 * 构建消息
 *
 * @return $this
 */
public function build()
{
    return $this->htmlView('emails.orders.shipped')
                ->attach('/path/to/file', [
                    'as' => 'name.pdf',
                    'mime' => 'application/pdf',
                ]);
}

/**
 * 构建消息
 *
 * @return $this
 */
public function build()
{
   return $this->htmlView('emails.orders.shipped')
               // 使用默认存储
               ->attachFromDefaultStorage('/path/to/file')
               // 使用指定存储
               ->attachFromStorage('s3', '/path/to/another_file');
}

/**
 * 构建消息
 *
 * @return $this
 */
public function build()
{
   return $this->htmlView('emails.orders.shipped')
               ->attachFromDefaultStorage('/path/to/file', 'name.pdf', [
                   'mime' => 'application/pdf'
               ])
               ->attachFromStorage('s3', '/path/to/another_file', 'name.zip', [
                   'mime' => 'application/zip'
               ]);
}

/**
 * 构建消息
 *
 * @return $this
 */
public function build()
{
    return $this->htmlView('emails.orders.shipped')
                ->attachData($this->pdf, 'name.pdf', [
                    'mime' => 'application/pdf',
                ]);
}

/**
 * 构建消息
 *
 * @return $this
 */
public function build()
{
    $this->htmlView('emails.orders.shipped');

    $this->withSwiftMessage(function ($message) {
        $message->getHeaders()
                ->addTextHeader('Custom-Header', 'HeaderValue');
    });
}



namespace App\Controller;

use App\Mail\OrderShipped;
use App\Model\Order;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use HyperfExt\Mail\Mail;

class OrderController
{
    /**
     * 发送给定的订单。
     */
    public function ship(RequestInterface $request, int $orderId): ResponseInterface
    {
        $order = Order::findOrFail($orderId);

        // 发送订单...

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

use HyperfExt\Mail\Mail;

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

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

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

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

Mail::to($request->user())
    ->cc($moreUsers)
    ->bcc($evenMoreUsers)
    ->later(new OrderShipped($order), 300); //延后  5 分钟发送

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

class OrderShipped extends Mailable implements ShouldQueue
{
    /**
     * 列队名称。
     *
     * @var string
     */
    public $queue = 'default';
}

$invoice = App\Models\Invoice::find(1);

return Mail::render(new App\Mail\InvoicePaid($invoice));

Route::get('mailable', function () {
    $invoice = App\Models\Invoice::find(1);

    return new App\Mail\InvoicePaid($invoice);
});

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

class User extends Model
{
    /**
     * 返回用户的特定区域信息
     *
     * @return string
     */
    public function getPreferredLocale(): string
    {
        return $this->locale;
    }
}

Mail::to($request->user())->send(new OrderShipped($order));
shell script
php bin/hyperf.php vendor:publish hyperf-ext/mail

"aws/aws-sdk-php": "~3.0"
shell script
php bin/hyperf.php gen:mail OrderShipped