简体   繁体   English

通过 Laravel 使用 Mailgun 从 HTML 表单发送数据

[英]Send data from a HTML Form with Mailgun through Laravel

Currently I'm working on a Laravel Project which has a contact form.目前我正在开发一个有联系表格的 Laravel 项目。 When the user fills in the contact form and clicks the action button, the data in the form should be sent to my current email.当用户填写联系表单并单击操作按钮时,表单中的数据应发送到我当前的电子邮件。

I followed a tutorial which showed me the way to send a test mail through mailtrap.io.我遵循了一个教程,该教程向我展示了通过 mailtrap.io 发送测试邮件的方法。 This worked completely fine as the content from the form got sent to the mailtrap mailbox.这完全正常,因为表单中的内容被发送到 mailtrap 邮箱。

Currently I changed my .env file to details I got on the mailgun dashboard using the sandbox domain which was created from Mailgun itself.目前,我使用从 Mailgun 本身创建的沙箱域将我的 .env 文件更改为我在 mailgun 仪表板上获得的详细信息。

MAIL_DRIVER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=THE_USERNAME
MAIL_PASSWORD=THE_PASSWORD
MAIL_ENCRYPTION=tls

I also have installed Guzzle and my controller looks like this我也安装了 Guzzle,我的控制器看起来像这样

<?php

namespace App\Http\Controllers;

use App\Mail\ContactFormMail; 
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;

class ContactFormController extends Controller
{
    public function create()
    {
        return view('contact.create');
    }

    public function store()
    {
        $data = request()->validate([
            'name' => 'required',
            'email' => 'required|email',
            'phone' => '',
            'subject' => 'required',
            'message' => 'required'
        ]);

        //dd(request()->all());

        // SEND EMAIL
        Mail::to('studiozalm040@gmail.com')->send(new ContactFormMail($data)); 

        //session()->flash('message', 'Bedankt voor je bericht. We zullen zo snel mogelijk contact met je opnemen.');
        return redirect('http://127.0.0.1:8000#contact')->with('message','Bedankt voor je bericht. We zullen zo snel mogelijk contact met je opnemen.');
    }
}

my ContactFormMail.php code is:我的 ContactFormMail.php 代码是:

<?php

namespace App\Mail;

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

class ContactFormMail extends Mailable
{
    use Queueable, SerializesModels;

    public $data; 

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

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->markdown('emails.contact.contact-form');
    }
}

Whenever I try to send the contact form, I receive no error or whatsoever.每当我尝试发送联系表时,我都没有收到任何错误或任何错误。 Yet the e-mail doesn't arrive.然而,电子邮件没有到达。 I must have been doing something wrong or forgot something...我一定是做错了什么或忘记了什么......

Thank you in Advance.先感谢您。

You are using the MAIL_DRIVER "mailgun" but are providing the SMTP credentials for mailgun.您正在使用 MAIL_DRIVER “mailgun”,但正在为 mailgun 提供 SMTP 凭据。

If you want to use the mailgun API, you should provide the following environment variables:如果你想使用 mailgun API,你应该提供以下环境变量:

MAIL_DRIVER=mailgun
MAILGUN_DOMAIN=x
MAILGUN_SECRET=y

If you instead want to use the smtp credentials you should change your MAIL_DRIVER from "mailgun" to "smtp" and then keep the rest of your current environment variabels.如果您不是要使用的SMTP凭证,则应该从“mailgun”到“SMTP”改变你的MAIL_DRIVER,然后保持当前的环境variabels休息。 Your environment variables would then look like this:您的环境变量将如下所示:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=THE_USERNAME
MAIL_PASSWORD=THE_PASSWORD
MAIL_ENCRYPTION=tls

The smtp configurations happens in config/mail.php but the API configuration (if you want to use the mailgun mail driver) happens in config/services.php smtp 配置发生在 config/mail.php 但 API 配置(如果你想使用 mailgun 邮件驱动程序)发生在 config/services.php

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

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