Home  >  Article  >  PHP Framework  >  Do you know how Laravel sends an email?

Do you know how Laravel sends an email?

藏色散人
藏色散人forward
2020-10-16 14:16:492002browse

The following is introduced to you by the Laravel tutorial column "Do you know how Laravel sends an email? 》, I hope it will be helpful to friends in need!

Introduction

In the previous chapter, we prepared a considerate form for sending emails, perfect Data verification, then in this article we explain how to send an email within laravel.

E-mail is very convenient, please do not abuse it.

Code Time

laravel integrates the popular and powerful SwiftMailer library, which encapsulates the underlying logic needed to send emails for us, so We only need to focus on the logic of sending and

how to prepare the content of the email.

laravel configuration fileconfig/mail.php Default smtp parameters:

'smtp' => [
    'transport' => 'smtp',
    'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
    'port' => env('MAIL_PORT', 587),
    'encryption' => env('MAIL_ENCRYPTION', 'tls'),
    'username' => env('MAIL_USERNAME'),
    'password' => env('MAIL_PASSWORD'),
    'timeout' => null,
    'auth_mode' => null,],

is mainly used to specify the transmission protocol, host address, port number, encryption method, user name and password, etc.

Due to overseas reasons, mailgun is used as the mail server by default, so as to prevent emails sent by our own mail server from

being identified as spam and affecting business processes.

Registering an account with a free quota can provide you with the corresponding number of emails sent each month. After applying, you can see the following code in the app/services.php configuration file:

'mailgun' => [
    'domain' => env('MAILGUN_DOMAIN'),
    'secret' => env('MAILGUN_SECRET'),
    'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'),],

We just declare the corresponding variables in the .env file.

Email sending class

To centralize the email sending logic, we need to render the incoming data and then send it to the user using email.

Use the following command to generate the email processing class:

php artisan make:mail ContactEmail

The generated file is located at app/Mail/ContactEmail.php, and the initial content is as follows:

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class ContactEmail extends Mailable
{
    use Queueable, SerializesModels;
    public function __construct()
    {
        //
    }
    public function build()
    {
        return $this->view('view.name');
    }
}

When instantiating, we need to receive some data:

public $contact;
public function __construct($contact){
    $this->contact = $contact;}

Sending a user-friendly email can greatly increase the user's stickiness. So before we use the view to render the email:

public function build(){
    return $this->to(config('mail.from.address'))->subject('HackerPair Inquiry')->view('emails.contact');}

Create the view file resources/views/emails/contact.blade.php, and briefly write the data rendering and format:

Hi,
A Laravel user has sent you a message.
Name: {{ $contact['name'] }}
E-mail: {{ $contact['email'] }}
Message: {{ $contact['msg'] }}

Send Email

In the previous article, we used the $contact variable to receive field values ​​from the request request body.

In the previous section, we prepared the template for sending emails. "Everything is ready, all we need is the east wind." Next is the main process logic for sending emails!

Remember to introduce App\Mail\ContactEmail in the header before use:

Mail::to(config('mail.support.address'))->send(new ContactEmail($contact));

Yes, just one line!

Written at the end

Overall, it is still very basic, and all functions are implemented with the help of third parties.

For example, the mail server has many customizable methods. There is also an email sending class, which can make many customized templates.

Those are all detailed aspects.

The above is the detailed content of Do you know how Laravel sends an email?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete