| 语法 | 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'=>"h3-user"); mail::send(['text'=>'mail'], $data, function($message) {
$message->to('h3.com@gmail.com', 'h3 h3')->subject
('laravel basic testing mail');
$message->from('xxxxxx@qq.com','h3 author');
});
echo "basic email sent. check your inbox.";
}
public function html_email(){
$data = array('name'=>"h3-user"); mail::send('mail', $data, function($message) {
$message->to('h3_com@qq.com', 'h3 h3')->subject
('laravel html testing mail');
$message->from('xxxxx@qq.com','h3 author');
});
echo "html email sent. check your inbox.";
}
public function attachment_email(){
$data = array('name'=>"h3-user"); mail::send('mail', $data, function($message) {
$message->to('h3.com@gmail.com', 'h3 h3')->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','h3 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文件中的表单方法的电子邮件地址是用来发送电子邮件的电子邮件地址。一般来说,它应是服务器上配置的电子邮件地址。



