语法 | void send(string|array $view, array $data, Closure|string $callback) |
---|---|
参数 |
|
返回值 | nothing |
描述 | 发送邮件 |
邮件可以发送HTML或文本。您可以通过传递一个数组指明发送邮件的类型,如下图所示的第一个参数。默认类型为HTML。如果您想发送纯文本邮件,然后使用以下语法。
Mail::send([‘text’=>’text.view’], $data, $callback);
第1步 - 现在要从Gmail帐户发送电子邮件,那么这里需要配置Laravel环境文件中的Gmail帐户 — .env 文件。Gmail帐户启用两步验证,创建一个应用程序并指定密码,如下图所示修改 .env 中的参数。
MAIL_DRIVER = smtp MAIL_HOST = smtp.qq.com MAIL_PORT = 587 MAIL_USERNAME = QQ邮箱地址,如:2211@qq.com MAIL_PASSWORD = QQ密码 MAIL_ENCRYPTION = tls
php artisan config:cache
php artisan make:controller MailController
第5步 - 复制下面的代码到 app/Http/Controllers/MailController.php 文件,具体代码如下:
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Mail; use App\Http\Requests; use App\Http\Controllers\Controller; class MailController extends Controller { public function basic_email(){ $data = array('name'=>"zyiz-User"); Mail::send(['text'=>'mail'], $data, function($message) { $message->to('zyiz.net@gmail.com', 'zyiz zyiz')->subject ('Laravel Basic Testing Mail'); $message->from('xxxxxx@qq.com','zyiz Author'); }); echo "Basic Email Sent. Check your inbox."; } public function html_email(){ $data = array('name'=>"zyiz-User"); Mail::send('mail', $data, function($message) { $message->to('zyiz_com@qq.com', 'zyiz zyiz')->subject ('Laravel HTML Testing Mail'); $message->from('xxxxx@qq.com','zyiz Author'); }); echo "HTML Email Sent. Check your inbox."; } public function attachment_email(){ $data = array('name'=>"zyiz-User"); Mail::send('mail', $data, function($message) { $message->to('zyiz.net@gmail.com', 'zyiz zyiz')->subject ('Laravel Testing Mail with Attachment'); $message->attach('D:\laravel\public\uploads\image.png'); $message->attach('D:\laravel\public\uploads\test.txt'); $message->from('xxxx@qq.com','zyiz Author'); }); echo "Email Sent with attachment. Check your inbox."; } }
resources/views/mail.blade.php
<h1>Hi, {{ $name }}</h1> <p>Sending Mail from Laravel.</p>
app/Http/routes.php
Route::get('sendbasicemail','MailController@basic_email'); Route::get('sendhtmlemail','MailController@html_email'); Route::get('sendattachmentemail','MailController@attachment_email');
http://localhost:8000/sendbasicemail
http://localhost:8000/sendhtmlemail
第11步 - 输出的画面将是这个样子。请检查您的收件箱是否看到HTML的电子邮件输出。
http://localhost:8000/sendattachmentemail
注 - 在MailController.php文件中的表单方法的电子邮件地址是用来发送电子邮件的电子邮件地址。一般来说,它应是服务器上配置的电子邮件地址。