如何配置laravel动态电子邮件发件人

时间:2018-06-08 06:23:06

标签: laravel email environment-variables

我的代码已经可以发送电子邮件了。但问题出在发件人的电子邮件名称中。如何根据用户输入使其动态化。

我的控制器:

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

我的mail.php

    'from' => [
    'address' => env('MAIL_FROM_ADDRESS', 'hello@example.com'),
    'name' => env('MAIL_FROM_NAME', 'Example'),
],

1 个答案:

答案 0 :(得分:0)

Laravel shutdownNow()类中from方法的第二个参数是Mailable。见this

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');
}

希望它有所帮助。