Home > PHP Framework > Laravel > body text

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

藏色散人
Release: 2020-10-19 14:25:11
forward
2642 people have browsed it

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
Copy after login
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');
Copy after login

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');
    }
Copy after login
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() !!}
    Copy after login

    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
Copy after login

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;}
Copy after login

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;
    ];}
Copy after login

##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;
Copy after login
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;);}
Copy after login

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!

Related labels:
source:learnku.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!