Mailgun电子邮件和Laravel

时间:2016-10-16 18:28:16

标签: laravel mailgun

我已经尝试过再次尝试,但我仍然坚持在注册时向新用户发送欢迎/验证令牌消息。我能够写入5.2和5.3中的日志文件,确认电子邮件令牌工作正常,但我无法向用户(我自己)发送实际的电子邮件。

有人可以看看这段代码并告诉我哪里出错了吗?我的域名已在Mailgun上验证。我困惑的一件事是如何从我创建的mailgun域创建子域?我想用' noreply@mydomain.com'但我在Mailgun注册的所有内容都是' mg.mydomain.com'我得到的许多错误之一是#34;给定[]的邮箱中的地址不符合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 trait:

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',

为长篇帖子道歉,但我想尽可能清楚,因为我不确定我哪里出错了

谢谢!

1 个答案:

答案 0 :(得分:0)

解决了! 我不得不将电子邮件提取到单独的变量

$ email = $ request-&gt;电子邮件; 邮件::至($电子邮件....);

立即行动!