Home > Article > PHP Framework > Teach you to use Laravel to send an email that "crosses the ocean"
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!
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 ContactControllerThen 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
{!! Form::open(['route' => 'contact.store']) !!}<p class="form-group"> {!! Form::label('name', 'Your Name') !!} {!! Form::text('name', null, ['class' => 'form-control']) !!}</p><p class="form-group"> {!! Form::label('email', 'E-mail Address') !!} {!! Form::text('email', null, ['class' => 'form-control']) !!}</p><p class="form-group"> {!! Form::textarea('msg', null, ['class' => 'form-control']) !!}</p>{!! Form::submit('Submit', ['class' => 'btn btn-info']) !!}{!! Form::close() !!}Note that the form is wrapped in
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 ContactFormRequestIn 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 [ 'name' => 'required', 'email' => 'required|email', 'msg' => 'required' ];}##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['name'] = $request->get('name');
$contact['email'] = $request->get('email');
$contact['msg'] = $request->get('msg');
// 邮件发送逻辑代码
return redirect()->route('contact.create');}
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!