简体   繁体   中英

Simple contact us form email in laravel using default template - No hint path defined for [mail] error

I am trying to create a simple contact us form email and use the existing default email template that comes shipped with laravel.

In my form I collect the following inputs from my users:

  • first_name
  • last_name
  • email
  • subject
  • message

I use the following FormRequest rules called SendEmailRequest to validate the input:

public function rules()
{
    return [
        'first_name' => ['required', 'string', 'min:3'],
        'last_name' => ['required', 'string', 'min:3'],
        'email' => ['required', 'email'],
        'subject' => ['required', 'in:' . implode(',', config('contact-us-subjects'))],
        'message' => ['required', 'string', 'min:10'],
    ];
}

This is the function on my controller that receives the request and attempts to send the email:

public function sendEmail(SendEmailRequest $sendEmailRequest)
{
    $validated = $sendEmailRequest->validated();

    $data['slot'] = view('mail.contact-us', $validated)->render();

    Mail::send('vendor.mail.html.message', $data, function($message) use($validated) {
        $message->from($validated['email'], $validated['first_name'] . ' ' . $validated['last_name']);
        $message->subject($validated['subject']);
        $message->to(config('mail.from.address'), config('mail.from.name'));
    });

    return redirect()
        ->back()
        ->with('status', 'Thanks, your email has been successfully sent to us');
}

When I run this; I am getting the following error:

Facade\Ignition\Exceptions\ViewException No hint path defined for [mail]. (View: C:\xampp\htdocs\MyApp\src\resources\views\vendor\mail\html\message.blade.php)

I am using Laravel 6.x with PHP 7.x. Any ideas what I might be doing wrong here?

I've solved the issue slightly differently. I had to build the mailable as markdown .

Step 1. Create a new mailable using this artisan command: php artisan make:mail ContactUs

Step 2. Ensure default to (similar to from address/name) is set on config/mail.php , for example:

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

Step 3. Modify the app/Mail/ContactUs.php like this:

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class ContactUs extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * @var array $data
     */
    private $data;

    /**
     * Create a new message instance.
     *
     * @param array $data
     */
    public function __construct(array $data)
    {
        $this->data = $data;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this
            ->from($this->data['email'], $this->data['first_name'] . ' ' . $this->data['last_name'])
            ->subject($this->data['subject'])
            ->markdown('mail.contact-us', [
                'messageBody' => $this->data['message'],
            ]);
    }
}

Step 4. Use the new mailable like this:

public function sendEmail(SendEmailRequest $sendEmailRequest)
{
    $validated = $sendEmailRequest->validated();

    Mail::send(new ContactUs($validated));

    return redirect()
        ->back()
        ->with('status', 'Thanks, your email has been successfully sent to us');
}

This appears to have achieved what I was originally after. The email was sent using the default template laravel ships with.

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