简体   繁体   English

Laravel 5联系表格不起作用

[英]Laravel 5 contact form not working

I don't have any errors but I did not receive any email from the form. 我没有任何错误,但是我没有从表格中收到任何电子邮件。

This is the view (reduced): 这是视图 (缩小):

{!! Form::open(['route' => 'contact_store', 'class' => 'form', 'method' => 'POST', 'files' => false]) !!}

        <input type="text" id="nombre" name="nombre" class="form-control">
        <input type="text" id="email" name="email" class="form-control">

        <textarea id="texto" name="texto" class="form-control"></textarea>

        {!! Form::submit('enviar', ['class' =>'btn btn-primary']) !!}

        {!! Form::close() !!}

The controller with the two methods: 控制器有两种方法:

namespace App\Http\Controllers\Web;

use App\Http\Requests\ContactRequest;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Mail;

class ContactController extends Controller
{
    public function create()
    {
        return view('web.nosotros.escribenos.escribenos');
    }
    public function store(Request $request)
    {
        $data = array(
            'nombre' => $request->nombre,
            'email' => $request->email,
            'texto' => $request->texto,
        );

        Mail::send('web.emails.contact', $data, function($message) use ($data){
            $message->from($data['email']);
            $message->to('my_email@domain.com');
            $message->subject('web contact');
        });

        return view('web.nosotros.escribenos.escribenos', compact('data'));
    }
}

The view web/emails/contact is only the format of the email: 查看网站/电子邮件/联系人仅是电子邮件的格式:

<h3>New message from the web!</h3>
<div>
    {{$texto}}
</div>
<p>from: {{ $email }}</p>

The routes in routes/web.php 在路由的路由 / web.php

Route::get('escribenos', ['as' => 'contact', 'uses' => 'ContactController@create']);
Route::post('escribenos', ['as' => 'contact_store', 'uses' => 'ContactController@store']);

In config/mail.php I have: config / mail.php中,我有:

'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'mydomain.com'),
'port' => env('MAIL_PORT', 25),
'from' => [
        'address' => env('MAIL_FROM_ADDRESS', 'webmaster@mydomain.com'),
        'name' => env('MAIL_FROM_NAME', 'webmaster'),
    ],
    'username' => env('MAIL_USERNAME'),
    'password' => env('MAIL_PASSWORD'),

And finally in .env : 最后在.env中

MAIL_DRIVER=smtp
MAIL_HOST=mydomain.com
MAIL_PORT=25
MAIL_USERNAME=webmaster@mydomain.com
MAIL_PASSWORD=my_password
MAIL_ENCRYPTION=null

All seem to be well, I don´t have any error but I don´t receive any email either. 一切似乎都很好,我没有任何错误,但是我也没有收到任何电子邮件。 Any idea? 任何想法? Thanks. 谢谢。

To test your emails you can utilize mailtrap.io and add the username/password etc into the .env settings. 要测试您的电子邮件,您可以利用mailtrap.io并将用户名/密码等添加到.env设置中。

Here is generally how I would set it out: 通常,这是我的设置方式:

Controller: 控制器:

use Illuminate\Support\Facades\Input;

    public function sendEmail(Request $request)
        {
            $data = array(
                'nombre' => $request->get('nombre'),
                'email' => $request->get('email'),
                'texto' => $request->get('texto')
            );

            // php artisan make:mail sendMessage - this needs to be ran in terminal / cmd
            Mail::to(config('mail.from.address'))->send(new sendMessage($data));
        }

SendMail.php:

private $data;

    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        $data = $this->data;
        return $this->view('web.emails.contact', compact('data'))
            ->subject('Web Contact')
            ->from(Input::get('email'));
    }

web/emails/contact.blade (basic example): 网络/电子邮件/contact.blade(基本示例):

<p>
    Hey there {{ config('mail.from.name') }}
</p>
<p>
    You have an email from {{ $data['nombre'] }} - email address {{ $data['email'] }}
</p>
<p>
    {!! $data['texto'] !!}
</p>

config('mail.from.name) & config('mail.from.address) can be adjusted within config/mail.php or you can simply remove these and add your own name / email address. config('mail.from.name)config('mail.from.address)可以在config/mail.php进行调整,也可以简单地删除它们并添加自己的姓名/电子邮件地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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