The introduction

In the previous chapter, we prepared a thoughtful form for sending an email, complete with data validation. In this article, we’ll show you how to send an email in Laravel.

Email is very convenient. Don’t abuse it.

Code time

Laravel integrates the popular and powerful SwiftMailer library to encapsulate the underlying logic needed to send emails, so we just need to focus on sending logic and preparing the content of emails.

Laravel config/mail.php

'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,].Copy the code

It is used to specify the transport protocol, host address, port number, encryption mode, user name, and password. For foreign reasons, mailgun is used as the mail server by default, so that the mail sent by our own mail server will not be identified as spam and affect the business process.

Sign up for a free account that offers a corresponding amount of mail per 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')],Copy the code

Let’s just declare the corresponding variables in the.env file.

Mail sending class

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

Generate the mail handling class with the following instruction:

php artisan make:mail ContactEmail
Copy the code

Generated file is located in the app/Mail/ContactEmail. PHP, original 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'); }}Copy the code

When instantiating, we need to receive some data:

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

Sending a friendly email to a user can greatly increase user engagement. So before we use view render mail:

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

Create a view file resources/views/emails/contact blade. PHP, rendering and briefly write data formats:

Hi,
A Laravel user has sent you a message.

Name: {{ $contact['name'] }}
E-mail: {{ $contact['email'] }}
Message: {{ $contact['msg']}}Copy the code

Send E-mail

In the last article, we used the $contact variable to receive field values from the body of the Request request. In the last section, we prepared the template for sending emails, “everything is ready but the east wind”, and then the main process logic for sending emails!

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

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

Yes, one line will do!

Write in the last

As a whole, it is still very early, and all functions are realized by the power of the third party. Mail servers, for example, have many customizable methods. There are also mail sending classes, you can do a lot of custom templates, those are the finer aspects.

Happy coding 🙂

I am a @programmer assistant, an original author in the IT field who focuses on programming knowledge and circle dynamics