Laravel 专题
您的位置:php > Laravel专题 > Laravel发送邮件
Laravel发送邮件
作者:--    发布时间:2019-11-20
laravel使用功能丰富免费的“swiftmailer”程序库来发送电子邮件。 使用这个库函数,我们可以轻松地发送电子邮件。电子邮件模板以视图同样的方式加载,这意味着可以使用 blade 语法并数据注入到你的模板。以下是发送函数的语法。
语法 void send(string|array $view, array $data, closure|string $callback)
参数
  • $view(string|array) – 包含电子邮件的视图的名称

  • $data(array) – 数组数据用来传递到视图

  • $callback – 接收邮件的实例,可自定义收件人,主题和邮件等方面的闭合回调

返回值 nothing
描述 发送邮件
在第三个参数, $callback封闭收到消息实例以及与实例,我们还可以调用下面函数并更改信息,如下图所示。
  • $message->subject('welcome to the h3 h3');
  • $message->from('email@example.com', 'mr. example');
  • $message->to('email@example.com', 'mr. example');
一些不太常用的方法包括 -
  • $message->sender('email@example.com', 'mr. example');
  • $message->returnpath('email@example.com');
  • $message->cc('email@example.com', 'mr. example');
  • $message->bcc('email@example.com', 'mr. example');
  • $message->replyto('email@example.com', 'mr. example');
  • $message->priority(2);
要附加或嵌入文件,可以使用以下方法 -
  • $message->attach('path/to/attachment.txt');
  • $message->embed('path/to/attachment.jpg');

邮件可以发送html或文本。您可以通过传递一个数组指明发送邮件的类型,如下图所示的第一个参数。默认类型为html。如果您想发送纯文本邮件,然后使用以下语法。

语法

mail::send([‘text’=>’text.view’], $data, $callback);
在此语法中,第一个参数需要一个数组。使用“text”为键,这个键对应的值是“name of the view”。 

示例

第1步 - 现在要从gmail帐户发送电子邮件,那么这里需要配置laravel环境文件中的gmail帐户 — .env 文件。gmail帐户启用两步验证,创建一个应用程序并指定密码,如下图所示修改 .env 中的参数。

.env

mail_driver = smtp
mail_host = smtp.qq.com
mail_port = 587
mail_username = qq邮箱地址,如:2211@qq.com
mail_password = qq密码
mail_encryption = tls
第2步 - 修改 .env 文件执行下面的两个命令来清除缓存,并重新启动laravel服务器之后。
php artisan config:cache
第3步 - 通过执行以下命令来创建一个名为 mailcontroller 的控制器。
php artisan make:controller mailcontroller
第4步 - 成功执行后,您会收到以下输出 -

第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.";
   }
}
第6步 - 将以下代码复制到resources/views/mail.blade.php 文件。

resources/views/mail.blade.php

<h1>hi, {{ $name }}</h1>
<p>sending mail from laravel.</p>
第7步 - 添加以下行添加到 app/http/routes.php 文件。

app/http/routes.php

route::get('sendbasicemail','mailcontroller@basic_email');
route::get('sendhtmlemail','mailcontroller@html_email');
route::get('sendattachmentemail','mailcontroller@attachment_email');
第8步 - 访问以下网址测试基本电子邮件。

http://localhost:8000/sendbasicemail

第9步 - 输出的画面将是这个样子。请检查您的收件箱是否看到基本的电子邮件输出。

第10步 - 访问以下网址来测试html电子邮件。

http://localhost:8000/sendhtmlemail

第11步 - 输出的画面将是这个样子。请检查您的收件箱是否看到html的电子邮件输出。


第12步 - 请访问以下网址使用附件测试html电子邮件。

http://localhost:8000/sendattachmentemail

第13步 - 输出画面将是这个样子。请检查您的收件箱看到有附件的html电子邮件输出。

打开邮件后:

注 - 在mailcontroller.php文件中的表单方法的电子邮件地址是用来发送电子邮件的电子邮件地址。一般来说,它应是服务器上配置的电子邮件地址。


网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册