laravel - 如何配置 laravel 动态邮件发件人
问题描述
我的代码已经可以发送电子邮件了。但问题在于发件人的电子邮件名称。如何根据用户输入使其动态化。
我的控制器:
class contactUsController extends Controller{
function index(Request $req){
$this->validate($req,
[
'email' => 'required|email',
'message'=>'min:10'
]);
$email = $req->input('email');
$messages = $req->input('message');
$data = array(
'email' => $email,
'bodyMessage' => $messages
);
Mail::send('Email.emailsendcontact', $data, function($message) use ($data){
$message->to('noticemyartheaddivision@gmail.com');
$message->from($data['email']);
$message->subject('NoticeMyArt Customer Service');
});
return redirect('home');
}}
我的环境
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=noticemyartheaddivision@gmail.com
MAIL_PASSWORD='my Password'
MAIL_ENCRYPTION=tls
我的邮件.php
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'hello@example.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],
解决方案
from
LaravelMailable
类中方法的第二个参数是name
. 看到这个。
// Illuminate\Mail\Mailable
/**
* Set the sender of the message.
*
* @param object|array|string $address
* @param string|null $name
* @return $this
*/
public function from($address, $name = null)
{
return $this->setAddress($address, $name, 'from');
}
因此,您可以使用以下方式设置发件人姓名和发件人电子邮件:
$message->from('some.email@here.com', 'Put Your Name Here');
希望能帮助到你。
推荐阅读
- javascript - 将信息传递给 Route 的子节点
- reactjs - React Js - 如果触发了 setState,则进行酶测试
- wordpress - 如何将 divi Menu.php 覆盖为子主题
- reactjs - 使用 useState 更新嵌套对象的值
- python - 使 Python 安装 praw 出现问题 - 语法错误?
- matlab - 我应该如何在 matlab 中更正我的程序?(排序应用程序)
- android - 如何在 Android API 级别 27 中禁用 SIM 插槽 1
- json - 从其中包含 json 格式的文本文件中正确读取数据
- python - 使用 Pylightxl 使用 Python 写入 Excel 工作表时出错
- omnet++ - 如何在 Omnet++ (Veins) 中运行不同的场景?