简体   繁体   English

Mailgun电子邮件和Laravel

[英]Mailgun email and Laravel

I have tried and tried again but I am stuck with sending the welcome / validate token message to a new user upon registration. 我已经尝试了一次又一次尝试,但是在注册时我一直将发送欢迎/验证令牌消息发送给新用户。 I was able to write to the log file in 5.2 and 5.3 and the confirm email token works fine, but I am unable to send an actual email to the user (myself). 我能够在5.2和5.3中写入日志文件,并且确认电子邮件令牌可以正常工作,但是我无法向用户(我自己)发送实际的电子邮件。

Could someone please have a look at this code and tell me where I'm going wrong? 有人可以看一下这段代码,然后告诉我我要去哪里了吗? My domain is validated on Mailgun. 我的域已在Mailgun上验证。 One of the things I am confused about is how to create a subdomain from the mailgun domain I created? 我感到困惑的一件事是如何从我创建的mailgun域中创建一个子域? I want to use something like 'noreply@mydomain.com' but all I have registered on Mailgun is 'mg.mydomain.com' One of the many errors I am getting is "Address in mailbox given [] does not comply with RFC 2822, 3.6.2." 我想使用“ noreply@mydomain.com”之类的东西,但我在Mailgun上注册的全部是“ mg.mydomain.com”。我遇到的许多错误之一是“给定的邮箱地址[]不符合RFC 2822 ,3.6.2。“ as Laravel or Mailgun is not recognizing the 'from' address I have created in config\\mail.php 由于Laravel或Mailgun无法识别我在config \\ mail.php中创建的“发件人”地址

.env : .env:

MAIL_DRIVER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=noreply@[mydomain].com // note, this is not configured on Mailgun I am trying to create subdomain here
MAIL_PASSWORD=[ "Default Password" on Mailgun dashboard -> encrpyted]
MAIL_ENCRYPTION=null
MAILGUN_DOMAIN=postmaster@mg.[mydomain].com // provided via Mailgun dashboard
MAILGUN_SECRET=key-[...API Key]

.RegistersUsers trait : .RegistersUsers特性:

use App\Mail\NewUserRegMail;
use Illuminate\Support\Facades\Mail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Auth\Events\Registered;

public function register(Request $request)
{

    $this->validator($request->all())->validate();

    event(new Registered($user = $this->create($request->all())));

    // $this->guard()->login($user);

// Send email and token to the NewUserRegMail class, this works
    Mail::to($request->user())->send(new NewUserRegMail($request->email, $request->_token));

    // redirect to login
    return redirect($this->redirectPath());
}

// Custom Mail class in App\\Mail.php .NewUserRegMail: // App \\ Mail.php中的Custom Mail类。NewUserRegMail:

<?php

namespace App\Mail;

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

class NewUserRegMail extends Mailable
{
    use Queueable, SerializesModels;

    protected $_token;
    protected $email;
    protected $token;
    // pass in User to construct

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

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from('mynewemail@mysubdomain.com')
                       ->subject('Register your email')
                    ->view('auth.emails.confirm')
                    ->with([
                        'to' => $this->email,
                        'token' => $this->_token
                    ]);
    }
}

.config\\mail.php .config \\ mail.php

 'driver' => env('MAIL_DRIVER', 'mailgun'),
    'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
    'port' => env('MAIL_PORT', 587),
    'from' => [
    'address' => 'noreply@mydomain.com',
    'name' => null
],
    'encryption' => env('MAIL_ENCRYPTION', 'tls'),
    'username' => env('MAIL_USERNAME'),
    'password' => env('MAIL_PASSWORD'),
    'sendmail' => '/usr/sbin/sendmail -bs',

Apologies for the long post but I wanted to make it as clear as possible as I am not sure where I'm going wrong 很长的道歉,但我想尽可能清楚地指出,因为我不确定我要去哪里

Thanks! 谢谢!

Solved ! 解决了 ! I had to extract email to a separate variable 我必须将电子邮件提取到一个单独的变量

$email = $request->email; $ email = $ request-> email; Mail::to($email....); Mail :: to($ email ....);

Works now! 现在工作!

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

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