PHP 7
Laravel 5.1
OS X El Capitan 10.11.4
Brief introduction
Laravel provides a concise API based on the popular Swiftmailer function library. Laravel provides drivers for SMTP, Mailgun, Mandrill, Amazon SES, PHP mail functions, and sendmail, allowing you to quickly start sending messages from your chosen local or cloud service. (excerpt Phphub translation document)
Configuration
The mail profile is config/mail.php
return [
Default Send Message Driver
' Driver ' => env (' Mail_driver ', ' SMTP '),
Send mail host Address
' Host ' => env (' mail_host ', ' smtp.mailgun.org '),
Send mail host port
' Port ' => env (' Mail_port ', 587),
Specify the mailbox address and user name for sending messages
' From ' => [' address ' => null, ' name ' => null],
Specify the Send mail protocol
' Encryption ' => env (' mail_encryption ', ' TLS '),
e-Mail login account
' Username ' => env (' Mail_username '),
Mailbox Login Password
' Password ' => env (' Mail_password '),
Specifies the driven command address when the driver is SendMail
' SendMail ' => '/usr/sbin/sendmail-bs ',
False send message does not log, true log does not send mail
' Pretend ' => false,
];
Writing Programs
Env
This article uses the QQ mailbox to carry on the test, first modifies the mailbox configuration
Mail_driver=smtp
Mail_host=smtp.qq.com
mail_port=465
Mail_username= (fill in QQ email account)
Mail_password= (fill in QQ email password)
Mail_encryption=ssl
Routing
/* Mail Management module * *
Route::get (' Email/send/{id} ', [
' As ' => ' backend.email.send ',
' Uses ' => ' emailcontroller@send ',
]);
Controller
New Controller
PHP Artisan Make:controller Backend/emailcontroller--plain
The controller code is as follows
<?php
namespace App\http\controllers\backend;
Use App\facades\userrepository;
Use Illuminate\http\request;
Use app\http\requests;
Use App\http\controllers\controller;
Use Mail;
Class Emailcontroller extends Controller
{
Public function Send (Request $request, $id)
{
$user = Userrepository::find ($id);
$result = Mail::send (' emails.test ', [' User ' => $user], function ($email) use ($user) {
$email->to (' 2794408425@qq.com ')->subject (' Hello world ');
});
if ($result) {
Echo ' send mail successfully ';
} else {
Echo ' failed to send message ';
}
}
}
New View
New View emails/test.blade.php, the code is as follows:
<title></title>
<body>
Hello, {{$user->name}}, this is a test message.
</body>
Executing code
Access the specified route in the browser, and then go to the mailbox to see if the message was sent successfully.
To see if a message was sent successfully