Home >Backend Development >PHP Tutorial >Markdown implementation of sending email code in Laravel5.5

Markdown implementation of sending email code in Laravel5.5

小云云
小云云Original
2018-02-28 14:06:132269browse

laravel supports markdown format for sending emails starting from version 5.4. I had time to try it on version 5.5 today. After using it, I found it very easy to use. I will make a simple record here.
Follow my steps below, you can also succeed, try it now!

Create Markdown template

php artisan make:mail Activate --markdown=emails.activate

After executing this command, the file Activate.php will be generated under the app/mail directory:

   namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Models\ActivateModel;
class Activate extends Mailable
{
    use Queueable, SerializesModels;
    private $activate;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct(ActivateModel $activate)
    {
        $this->activate = $activate;
    }
    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
      return $this->markdown('emails.activate')->with('activate', $this->activate);
    }
}

and the template will be generated File, in resource/views/emails/activate.blade.php:

  @component('mail::message')
# 欢迎注册使用 Laravel
点击下面按钮进行激活。
@component('mail::button', ['url' => 'http://www.laravel.com'])
激活
@endcomponent
Thanks,<br>
{{ config(&#39;app.name&#39;) }}
@endcomponent


Email configuration

Sending emails requires basic configuration To support, configure it in the .env file. Here I use the 163 mailbox as an example:

   MAIL_DRIVER=smtp
MAIL_HOST=smtp.163.com
MAIL_PORT=25
MAIL_USERNAME=账号
MAIL_PASSWORD=密码
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=全局发件人地址
MAIL_FROM_NAME=全局发件人名称

Send call

Introduce Activate where you need to send emails, and use the Mail Facade The to method is called, I will make a simple route for testing here:

   # routes/web.php
Route::get(&#39;sendEmail&#39;, &#39;IndexController@sendEmail&#39;);
``` 
```php
# app/Http/Controllers/IndexController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\Mail\Activate;
class IndexController extends Controller
{
  public function sendEmail()
  {
    // ... code
    // 调用方式
    Mail::to(&#39;demo@example.com&#39;)->send(new Activate($activate));
  }
}


Execute the test

Execute the command in the project root directory:

php artisan serve

After starting the server, open the browser and enter the URL http://localhost:8000/sendEmail, and then check whether the sending mailbox has received the email.

Related recommendations:

implementation code of markdown document management tool in php

A brief introduction to markdown editor

markdown How to get the text content of markdown

The above is the detailed content of Markdown implementation of sending email code in Laravel5.5. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn