【发布时间】:2017-02-25 16:47:42
【问题描述】:
我已经尝试并再次尝试,但我坚持在注册时向新用户发送欢迎/验证令牌消息。我能够在 5.2 和 5.3 中写入日志文件,并且确认电子邮件令牌工作正常,但我无法向用户(我自己)发送实际电子邮件。
有人可以看看这段代码并告诉我哪里出错了吗?我的域在 Mailgun 上得到验证。我感到困惑的一件事是如何从我创建的 mailgun 域创建子域?我想使用“noreply@mydomain.com”之类的东西,但我在 Mailgun 上注册的只是“mg.mydomain.com” 我遇到的许多错误之一是“给定邮箱中的地址 [] 不符合 RFC 2822 ,3.6.2。”因为 Laravel 或 Mailgun 无法识别我在 config\mail.php 中创建的“发件人”地址
.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 特征:
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());
}
// App\Mail.php 中的自定义邮件类 .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
'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',
为这篇长文道歉,但我想尽可能清楚地说明,因为我不确定我哪里出错了
谢谢!
【问题讨论】: