首页 文章

Mailgun电子邮件和Laravel

提问于
浏览
0

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

有人可以看看这段代码并告诉我哪里出错了?我的域名已在Mailgun上验证 . 我困惑的一件事是如何从我创建的mailgun域创建子域?我想使用'noreply@mydomain.com'之类的东西,但我在Mailgun上注册的所有内容都是'mg.mydomain.com'我得到的许多错误之一是“给出[]邮箱中的地址不符合RFC 2822 ,3.6.2 . “因为Laravel或Mailgun没有识别我在config \ mail.php中创建的'from'地址

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

为长篇文章道歉,但我想尽可能清楚,因为我不知道我哪里出错了

谢谢!

1 回答

  • 0

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

    $ email = $ request-> email;邮件::以($电子邮件....);

    现在就行!

相关问题