laravel5.4 发送SMTP邮件

news/2024/7/5 12:18:42 标签: php, json, 数据库

https://blog.csdn.net/qq_35843527/article/details/77880631

 

Lumen / Laravel 5.4 使用网易邮箱 SMTP 发送邮件

获取网易邮箱的服务器和授权码:

登录网易邮箱 (http://mail.163.com/),

    1. 获取服务器地址:
      点击【设置】 > 【POP3/SMTP/IMAP】:

服务器地址:

 POP3服务器: pop.163.com
SMTP服务器: smtp.163.com
IMAP服务器: imap.163.com
    1. 获取客户端授权密码

授权码
授权码是用于登录第三方邮件客户端的专用密码。
适用于登录以下服务: POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务。

点击【设置】 > 【客户端授权密码】
点击【开启】, 设置一个授权码, 比如本例中将授权码设置为: mailPASSWORD

配置 env 文件:

在配置文件 .env文件,新增以下配置:

    MAIL_DRIVER=smtp
    MAIL_HOST=smtp.163.com
    MAIL_PORT=25
    MAIL_USERNAME=cnwytnet@163.com
    MAIL_PASSWORD=mailPASSWORD
    MAIL_ENCRYPTION=null
    MAIL_FROM_ADDRESS=cnwytnet@163.com
    MAIL_FROM_NAME=cnwytnet

Lumen 项目

由于 Lumen 是简化版的 Laravel, 需要增加以下发邮件的模块。

  • 需要添加 illuminate/mail 模块:

修改composer.json 文件中 require 部分配置如下:

    "require": {
    "php": ">=5.6.9",
    "laravel/lumen-framework": "5.4.*",
    "vlucas/phpdotenv": "~2.2",
    "guzzlehttp/guzzle": "^6.2",
    "predis/predis": "^1.1",
    "illuminate/redis": "^5.4",
    "illuminate/mail":"5.4.*"
    }

执行 composer up.

  • 需要增加mail.php配置文件:

确保Luemn项目中存在 app/config/mail.php 配置文件。
若不存在可以从 Laravel 代码中复制一份。

创建发邮件脚本

  • 创建脚本文件 app/Console/Command/SendMailCommand.php
<?php
namespace App\Console\Commands;
 
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;
 
class SendMailCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'demo:SendMail';
 
/**
* The console command description.
*
* @var string
*/
protected $description = '命令行-测试脚本-SendMail';
 
/**
* constructor
*/
public function __construct()
{
parent::__construct();
}
 
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$content = '这是一封来自Laravel的测试邮件.';
$toMail = 'wangtom365@qq.com';
 
Mail::raw($content, function ($message) use ($toMail) {
$message->subject('[ 测试 ] 测试邮件SendMail - ' .date('Y-m-d H:i:s'));
$message->to($toMail);
});
}
}

 

  • 将脚本文件加入到 app/Console/Kernel.php 中:
  1.  protected $commands = [
    Commands\SendMailCommand::class, //测试发邮件脚本
    ];
    

     

执行发邮件操作

  • 查看脚本, 可以看到我们新加的脚本命令 demo:SendMail:
    $ php artisan
    demo
    demo:SendMail 命令行-测试脚本-SendMail
  • 执行发送邮件脚本:
$ php artisan demo:SendMail

不出意外的话,邮件发送成功。查看发件人的发件箱,或者查看收件人的收件箱,确认一下吧。

其他

  • 邮件地址 MAIL_FROM_ADDRESS 必须和 MAIL_USERNAME一致,否则报错:
  1. [Swift_TransportException]
  2. Expected response code 250 but got code "553", with message "553 Mail from must equal authorized user"
  • 不填授权码 MAIL_PASSWORD 或者 MAIL_PASSWORD 错误,报错:
 [Swift_TransportException]
Failed to authenticate on SMTP server with username "cnwytnet@163.com" using 2 possible authenticators 

可以将邮件驱动改成 MAIL_DRIVER=log, 就可以在本地日志中看到邮件内容了,这在测试的时候会很有用。

比如,在配置.env中,修改邮件驱动为MAIL_DRIVER=log,将会把邮件发送内容保存到 storage/logs/laravel.log 中。 内容如下:

[2017-04-01 06:12:19] local.DEBUG: Message-ID: <727877e080177bbb349b98a869f5b20f@swift.generated>
Date: Sat, 01 Apr 2017 06:12:19 +0000
Subject: [ =?utf-8?Q?=E6=B5=8B=E8=AF=95?= ] SendMail - 2017-04-01 06:12:19
From: SendMailTEST <cnwytnet@163.com>
To: wangtom365@qq.com
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
 
这是一封来自Laravel的测试邮件.

 

END.

参考链接:

https://laravel.com/docs/5.4/mail

http://laravelacademy.org/post/1986.html


http://www.niftyadmin.cn/n/975996.html

相关文章

bash变量

2019独角兽企业重金招聘Python工程师标准>>> 1.用户自定义变量 在bash中&#xff0c;变量默认都是字符串型&#xff0c;若要进行数值运算&#xff0c;则需要指定变量类型为数值类型 给变量赋值时等号两边不能有空格&#xff0c;如果变量本身有空格&#xff0c;则需要…

FlaskWeb开发:基于Python的Web应用开发实战pdf

下载地址&#xff1a;网盘下载 本书不仅适合初级Web开发人员学习阅读&#xff0c;更是Python程序员用来学习高级Web开发技术的优秀参考书。 • 学习Flask应用的基本结构&#xff0c;编写示例应用&#xff1b; • 使用必备的组件&#xff0c;包括模板、数据库、Web表单和电子邮件…

anaconda下载及vscde

一.anaconda的下载&#xff08;最简单的方法&#xff09; https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-2020.07-Windows-x86_64.exe直接点击&#xff0c;直接下载。 选择添加path变量&#xff08;新手最好点&#xff0c;自己配太麻烦了&#xff09; 安装…

MariaDB/Mysql 批量插入 批量更新

2019独角兽企业重金招聘Python工程师标准>>> 1. 批量插入 insert into ............ insert into table_name (id, name, age) values (1,乔峰,21),(2,段誉,22),(3,虚竹,23); 2. 批量更新(存在就更新, 不存在就插入) insert into ............ on duplicate key upd…

手把手教你把一篇pdf英文文献瞬间翻译成doc格式的中文

2019独角兽企业重金招聘Python工程师标准>>> https://wenku.baidu.com/view/837d09bbb8f67c1cfad6b8ec.html 转载于:https://my.oschina.net/soho00147/blog/875509

Windows远程桌面连接 出现身份错误 要求的函数不受支持

Windows远程桌面连接 出现身份错误 要求的函数不受支持 https://www.cnblogs.com/Amaya/p/9018653.html 原因 CVE-2018-0886 的 CredSSP 更新 将默认设置从“易受攻击”更改为“缓解”的更新。 摘要 凭据安全支持提供程序协议 (CredSSP) 是处理其他应用程序的身份验证请求的身份…

【hbuilder】如何根据Geolocation获得的坐标获取所在城市?

第一步通过mui.plusReady【表示页面加载事件】调用hbuilder提供的百度定位mui.plusReady(function(){plus.geolocation.getCurrentPosition( geoInf, function ( e ) {},{geocode:true,provider:amap});});第二步通过geolnf 方法来获取具体的定位信息function geoInf( position…

如何理解阻塞和非阻塞同步和异步

1.同步与异步同步和异步关注的是消息通信机制&#xff0c;所谓同步&#xff0c;就是在发出一个调用时&#xff0c;在没有得到结果之前&#xff0c;该调用就不返回。但是一旦调用返回&#xff0c;就得到返回值了。换句话说&#xff0c;就是由调用者主动等待这个调用的结果。而异…