我正在一个简单的laravel网站上工作。 我需要发送电子邮件(联系表格)。我用mailtrap测试了我的代码,但是没有用。我花了一些时间,但找不到解决方案。
我的联系表格:
<form method="post" action="{{ route('sendContactEmail') }}">
@csrf
<div class="input_contact">
<input type="text" name="name" class="form_name">
</div>
<div class="input_contact">
<input type="email" name="email" class="form_email">
</div>
<div class="input_contact">
<input type="text" name="phone" class="form_phone">
</div>
<div class="input_contact">
<input type="text" name="text" class="form_message">
</div>
<button type="submit" class="submit_contact"> Envoyer </button>
</form>
接收请求的我的控制器:
public function sendContactEmail ( Request $request ){
$this->validate($request, [
'name' => 'required|max:255|string',
'email' => 'required|email|max:255',
'phone' => 'required|max:255|string',
'message' => 'required'
]);
Notification::route('mail','test@gmail.com')
->notify(new SendContactNotification($request));
return $request;
}
和我的SendContactNotification:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class SendContactNotification extends Notification
{
use Queueable;
protected $request;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($request)
{
$this->request = $request;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->subject('Contact via escapehome.ch')
->line('Nom : ' . $this->request->name)
->line('Téléphone : ' . $this->request->phone)
->line('Message : '. $this->request->message)
->action('Répondre', url('mailto:'.$this->request->email));
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
对于我的.env:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=465
MAIL_USERNAME=( my username mailtrap )
MAIL_PASSWORD=( my password mailtrap )
提交表单时,我没有错误消息。 谢谢。