简体   繁体   中英

How to get the email senders name from the contact form - Laravel Mail

How can i change the Mail Subject, Title and the senders name from the form input fields,

Here's my contact form

<h1>Contact</h1>
<form action="contact/store" method="post">
    @csrf
    <label>Name</label>
<input type="text" name="name" placeholder="Name">
<br>
<label>Email</label>
<input type="email" name="email" placeholder="Email">
<br>
<label>Message</label>
<textarea name="message" cols="30" rows="10"></textarea>
<button type="submit">Submit</button>
</form>

Here's my Mail controller

public function store(Request $request)
{
    $name = $request->name;
    $email = $request->email;
    $textmessage = $request->message;

    $data = array('name' => $name, 'email' => $email, 'textmessage' => $textmessage);
    Mail::send('mail', $data, function ($message) {
        $message->to('jareerzeenam.29@gmail.com', 'Email Title')->subject('Test Subject 2');
        $message->from('test@gmail.com', 'Test Name 2');
    });
    echo "HTML Email Sent. Check your inbox.";
}

Here's my mail.blade.php

    <h1>Hi, i'm {{ $name }}</h1>
<p>Email :{{ $email }}</p>
<p>Message : {{ $textmessage }}</p>

Here's how i get the email as

电子邮件屏幕截图

How can i get the name from the contact form input field to my email instead of the static name i have given in the controller, hope my question is understandable.

Instead of using

->to('jareerzeenam.29@gmail.com', 'Email Title')

use

->to([['mail' => 'jareerzeenam.29@gmail.com', 'name' => 'Email Title']])

So, to() accepts array of receivers, and a receiver can be an array with name and mail.

You just need to assign name and email , I didn't find subject, so I left it as default.

Try this:

public function store(Request $request)
{
    $name = $request->name;
    $email = $request->email;
    $textmessage = $request->message;

    $data = array('name' => $name, 'email' => $email, 'textmessage' => $textmessage);
    Mail::send('mail', $data, function ($message) use ($name, $email) {
        $message->to('jareerzeenam.29@gmail.com', 'Email Title')->subject('Test Subject 2');
        $message->from($email, $name);
    });
    echo "HTML Email Sent. Check your inbox.";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM