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));
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');
}
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();