Dazzle
Dazzle

Reputation: 3083

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).

Could someone please have a look at this code and tell me where I'm going wrong? My domain is validated on Mailgun. One of the things I am confused about is how to create a subdomain from the mailgun domain I created? I want to use something like '[email protected]' 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." as Laravel or Mailgun is not recognizing the 'from' address I have created in 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());
}

// Custom Mail class in 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('[email protected]')
                       ->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' => '[email protected]',
    '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!

Upvotes: 0

Views: 458

Answers (1)

Dazzle
Dazzle

Reputation: 3083

Solved ! I had to extract email to a separate variable

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

Works now!

Upvotes: 0

Related Questions