PHP code example of yzh52521 / think-mailer

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

    

yzh52521 / think-mailer example snippets


/**
 * 构建消息
 *
 * @return $this
 */
public function build()
{
    return $this->from('[email protected]', 'Example')
                ->view('emails/orders/shipped');
}


'from' => ['address' => '[email protected]', 'name' => 'App Name'],

'reply_to' => ['address' => '[email protected]', 'name' => 'App Name'],

/**
 * Build the message.
 *
 * @return $this
 */
public function build()
{
    return $this->view('emails/orders/shipped');
}

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




namespace app\mail;

use app\model\Order;
use think\queue\ShouldQueue;
use yzh52521\mail\Mailable;

class OrderShipped extends Mailable
{

    /**
     * 订单实例.
     *
     * @var \app\model\Order
     */
    public $order;

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

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

<div>
    Price: { $order->price }
</div>



namespace app\mail;

use app\model\Order;
use think\queue\ShouldQueue;
use yzh52521\mail\Mailable;

class OrderShipped extends Mailable
{

    /**
     * 订单实例.
     *
     * @var \app\model\Order
     */
    protected $order;

    /**
     * 创建消息实例.
     *
     * @param  \app\model\Order  $order
     * @return void
     */
    public function __construct(Order $order)
    {
        $this->order = $order;
    }

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

<div>
    Price: { $orderPrice }
</div>

/**
 * Build the message.
 *
 * @return $this
 */
public function build()
{
    return $this->view('emails/orders/shipped')
                ->attach('/path/to/file');
}

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

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

   ->with([
        'date' => date('Y-m-d H:i:s'),     
        'cid:image' => '/path/to/image1.jpg',
        // 'cid:image' => [fopen('/path/to/image1.jpg','r')],
        // 'cid:image' => [fopen('/path/to/image1.jpg','r'),'image.jpg'],
        // 'cid:image' => [fopen('/path/to/image1.jpg','r'),'image.jpg','image/jpg'],
     ])



namespace app\controller;

use app\mail\OrderShipped;
use app\model\Order;
use yzh52521\facade\Mail;

class OrderShipmentController 
{
    /**
     * 发送给定的订单.
     *
     * @param  \think\Request  $request
     */
    public function store(Request $request)
    {
        $order = Order::findOrFail($request->order_id);

        // Ship the order...

        Mail::send(new OrderShipped($order));
    }
}

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

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

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

class OrderShipped extends Mailable 
{
    public function __construct(Order $order)
    {
        $this->order = $order;
        $this->delay =5;
    }
}
或者

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

class OrderShipped extends Mailable 
{
    public function __construct(Order $order)
    {
        $this->order = $order;
        $this->delay =5;
        $this->queue ='emails';
        $this->connection ='sqs';
    }
}

或者

$message = (new OrderShipped($order))
                ->onConnection('redis')
                ->onQueue('emails');
Mail::to($request->user())
    ->cc($moreUsers)
    ->bcc($evenMoreUsers)
    ->queue($message);


use think\queue\ShouldQueue;
use yzh52521\mail\Mailable;

class OrderShipped extends Mailable implements ShouldQueue
{
    //
}

 $order = Order::find(1);
 return (new OrderShipped($order))->render();

use yzh52521\mail\events\MessageSending;
use yzh52521\mail\events\MessageSent;
return [
    'listen' => [
        MessageSending::class => [
            app\listeners\LogSendingMessage::class,
        ],
        MessageSent::class => [
            app\listeners\LogSentMessage::class,
        ],
    ],
]

composer 

php think make:mail OrderShipped

use Symfony\Component\Mime\Email;

public function build()
{
    $this->view('emails/orders/shipped');

    $this->withSymfonyMessage(function (Email $message) {
        $message->getHeaders()->addTextHeader(
            'Custom-Header', 'Header Value'
        );
    });

    return $this;
}