Home  >  Article  >  PHP Framework  >  Teach you to use Laravel to send an email that "crosses the ocean"

Teach you to use Laravel to send an email that "crosses the ocean"

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

The following tutorial column will introduce to you how to use Laravel to send an email "across the ocean". I hope it will be helpful to friends in need!

##Introduction

Email has greatly improved people's efficiency since its birth. Traditional The green mailbox method of sending letters has been replaced by bundles of communication messages transmitted on fiber optic cables. E-mail also brings the intrusion of spam messages. With the popularity of domestic social APPs, e-mails are gradually becoming more professional.

In this issue, we will not talk about how to send an email. Let’s first prepare a form and prepare the data required for the email.

Create Form form

First use the command line to create a restful style controller:

php artisan make:controller ContactController
Then routes/web.php

Register the resource class routing address in the routing file:

Route::get('contact', 'ContactController@create')->name('contact.create');
Route::post('contact', 'ContactController@store')->name('contact.store');

We first implement the create method to render the view of the form:

namespace App\Http\Controllers;use Illuminate\Http\Request;
use App\Http\Requests;
class ContactController extends Controller {
    public function create()
    {
        return view('contact.create');
    }
Then use the FormBuilder we talked about to create A form for sending an email. The main fields are

name: the sender’s name

  • email: the recipient’s email address

  • msg: Email content

  • The following are the input fields of the form in the view file:

    {!! Form::open([&#39;route&#39; => &#39;contact.store&#39;]) !!}<p class="form-group">
        {!! Form::label(&#39;name&#39;, &#39;Your Name&#39;) !!}
        {!! Form::text(&#39;name&#39;, null, [&#39;class&#39; => &#39;form-control&#39;]) !!}</p><p class="form-group">
        {!! Form::label(&#39;email&#39;, &#39;E-mail Address&#39;) !!}
        {!! Form::text(&#39;email&#39;, null, [&#39;class&#39; => &#39;form-control&#39;]) !!}</p><p class="form-group">
        {!! Form::textarea(&#39;msg&#39;, null, [&#39;class&#39; => &#39;form-control&#39;]) !!}</p>{!! Form::submit(&#39;Submit&#39;, [&#39;class&#39; => &#39;btn btn-info&#39;]) !!}{!! Form::close() !!}

    Note that the form is wrapped in
  • Between open
and

close

.

Verify data

After the form is created, we need to write a method to receive the form data. Before receiving and processing, the data must be valid sex to verify. Remember what we talked about earlier, using the FormRequest object to validate form fields.

Create a form validator on the command line:

php artisan make:request ContactFormRequest

In order to simplify the logic, we need to add all the code that calls the validator, and force verification regardless of any permissions. Modify the

authorize

method:

public function authorize(){
    return true;}

Then define the verification rules, the built-in rules are enough:

public function rules(){
    return [
        &#39;name&#39; => &#39;required&#39;,
        &#39;email&#39; => &#39;required|email&#39;,
        &#39;msg&#39; => &#39;required&#39;
    ];}

##Combined

With the form submitted and the validator, we then need to process the data and write it to the database. Write the following code in the store method of the controller ContactController.

Introduce the validator in the header:

use App\Http\Requests\ContactFormRequest;
Use dependency injection to call:
public function store(ContactFormRequest $request){
    $contact = [];
    $contact[&#39;name&#39;] = $request->get(&#39;name&#39;);
    $contact[&#39;email&#39;] = $request->get(&#39;email&#39;);
    $contact[&#39;msg&#39;] = $request->get(&#39;msg&#39;);

    // 邮件发送逻辑代码
    return redirect()->route(&#39;contact.create&#39;);}

Write at the end

Sending emails is a matter of integrating the operating system and the application program. The logic code for sending emails in the third section above will be given in the next article. Although this article is small, it describes all aspects from form to verification to data interaction, from which you can understand the laravel processing process.                                                                                                                        

The above is the detailed content of Teach you to use Laravel to send an email that "crosses the ocean". 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