0

I have a laravel project at hand. I am getting error Address in mailbox given [] does not comply with RFC 2822, 3.6.2. Here is the file where I am getting the error: App\Http\Controllers\Api\AuthController.php It shows the error from this file, but I guess the sending part is the main source of the error. In other words, since the mail sending process is used here, I understand that it shows the error here. I am sharing the codes of the relevant files below. You can look there.

The line where it shows the error: Mail::to($email)->send(new MagicLink($auth, $link));

AuthController.php


namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests\Api\RegisterRequest;
use App\Http\Resources\Api\UserResource;
use App\Mail\Auth\MagicLink;
use App\CreatorApplicationForm;
use App\User;
use App\UserNotificationToken;
use Auth;
use Cache;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Password;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Mail;

class AuthController extends Controller
{
    public function login(Request $request)
    {
        $request->validate([
            'email' => 'required|string|email',
            'password' => 'required|string',
            'remember_me' => 'boolean'
        ]);

        $credentials = request(['email', 'password']);

        if (!Auth::attempt($credentials))
            return response()->json([
                'message' => 'You have entered incorrect username or password. Please try again.'
            ], 401);

        $user = $request->user();

        foreach ($request->user()->tokens as $token){
            $token->revoke();
        }

        $tokenResult = $user->createToken('Personal Access Token');
        $token = $tokenResult->token;

        if ($request->remember_me)
            $token->expires_at = Carbon::now()->addMonths(12);

        $token->save();

        return response()->json([
            'access_token' => $tokenResult->accessToken,
            'token_type' => 'Bearer',
            'expires_at' => Carbon::parse($tokenResult->token->expires_at)->toDateTimeString()
        ]);
    }

    public function saveUserNotificationToken(Request $request)
    {
        DB::table('user_notification_tokens')->where('user_id', '=', $request->user()->id)->delete();
        UserNotificationToken::create(['user_id' => $request->user()->id, 'token' => $request->token]);
        return true;
    }
    public function readNotifications(Request $request)
    {
        $user = $request->user();
        DB::table('user_notifications')
            ->where('user_id', '=', $user->id)
            ->whereIn('id', $request->notification_id_list)
            ->update(['isRead' => true]);

        return collect($user->unReadNotificationsHistory())->count();
    }

    public function getNotifications(Request $request)
    {
        $user = $request->user();
        return $user->notificationsHistory();
    }
    public function getUser(Request $request)
    {
        $user = $request->user();
        $user['last_listened_data'] = $user->getLastListenedCourse();
        $followings = DB::table('user_creator_follow_pivots')->select('followed_id')->where('follower_id', $user->id)->get();
        $user['followings'] = $followings->map(function ($fol) {
            return $fol->followed_id;
        });
        $user['notifications_count'] = collect($user->unReadNotificationsHistory())->count();

        $application = CreatorApplicationForm::where('email','=',$user->email)->get()->first();
        $user['isApplied'] = ($application!=null); 

        return response()->json($user);
    }

    public function register(Request $request)
    {
        $request->validate([
            'name' => 'required|string',
            'email' => 'required|string|email|unique:users',
            'password' => 'required|string|confirmed'
        ]);

        $user = new User([
            'name' => $request->name,
            'email' => $request->email,
            'password' => bcrypt($request->password),
            'role_id' => 4,
            'birth_date' => $request->birth_date,
        ]);

        $user->save();


        return response()->json([
            'message' => 'Successfully created user!'
        ], 201);
    }

    public function logout(Request $request)
    {
        $request->user()->token()->revoke();
        return response()->json([
            'message' => 'Successfully logged out'
        ]);
    }


    public function forgotPassword(Request $request)
    {
        $input = $request->only('email');
        $validator = Validator::make($input, [
            'email' => "required|email"
        ]);
        if (User::where('email', '=', $input)->count() == 0) {
            return response()->json(["message" => "There is no user with this email. Are you sure you signed up with it?"], 403);
        }
        if ($validator->fails()) {
            return response()->json($validator->errors());
        }
        $response = Password::sendResetLink($input);

        $message = $response == Password::RESET_LINK_SENT ? 'Mail send successfully' : GLOBAL_SOMETHING_WANTS_TO_WRONG;

        return response()->json($message);
    }

    public function passwordReset(Request $request)
    {
        $request->validate([
            'password' => 'required|string|confirmed'
        ]);
        $user = $request->user();
        $user->password = bcrypt($request->password);
        $user->save();

        return response()->json([
            'message' => 'Successfully Updated User Password!'
        ], 200);
    }

    public function updateUser(Request $request)
    {
        $request->validate([
            'name' => 'required'
        ]);
        $user = $request->user();

        $user->update([
           'name' => $request->name
        ]);

        return response()->json([
            'message' => 'Successfully Updated!'
        ], 200);
    }

    public function magicEmail(Request $request)
    {
        $request->validate([ 
            'email' => 'required|email', 
        ]); 

        $email = $request->email;
        $auth = app('firebase.auth'); 
        $anonymous = Auth::user(); 

        $actionCodeSettings = [
            'continueUrl' => 'http://app.omnicourse.io/magicauth?unique=' . $anonymous->id . '&email=' . $email,  
            'handleCodeInApp' => true, 
            'androidPackageName' => 'io.omnicourse.app',
            'androidInstallApp' => true,
            'iOSBundleId' => 'io.omnicourse.app',
        ]; 

        $link = $auth->getSignInWithEmailLink($email, $actionCodeSettings); 
        //$auth->sendSignInWithEmailLink($email, $actionCodeSettings); 

        Mail::to($email)->send(new MagicLink($auth, $link));

        if($email == 'appletest@omnicourse.io') 
        {
            $items = Cache::get('apple.dev.mail', []);
            array_push($items, array(
                'time' => Carbon::now(),
                'email' => $email,
                'content' => null,
                'url' => $link,
            ));
            Cache::put('apple.dev.mail', $items);
        }

        if (Mail::failures()) { 
            return response()->json([
                'status' => false,
                'message' => 'There is a problem. Try again later'
            ], 200);
        }  else {
            return response()->json([
                'status' => true,
                'message' => 'Successfully sended mail!'
            ], 200);
        }
    }
    
    
}

Magiclink.php


namespace App\Mail\Auth;

use App\Course;
use Kreait\Firebase\Auth;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class MagicLink extends Mailable
{
    use Queueable, SerializesModels;

    public $anonymous;
    public $link;
    public $fromName = 'Omnicourse';
    public $fromEmail = 'noreply@omnicourse.io';

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

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from($this->fromEmail, $this->fromName)
            ->view('email.auth.magic_link') 
            ->text('email.auth.magic_link_plain')
            ->subject('Sign in to Omnicourse');
    }
}
iguner
  • 51
  • 8
  • What did you try to resolve the problem? This looks pretty similar to https://stackoverflow.com/questions/73566541/i-get-an-error-address-in-mailbox-given-does-not-comply-with-rfc-2822-3-6-2 – Nico Haase Sep 02 '22 at 12:46
  • similar [to](https://stackoverflow.com/questions/21530566/address-in-mailbox-given-does-not-comply-with-rfc-2822-3-6-2-when-email-is). – francisco Sep 02 '22 at 12:55
  • @NicoHaase I guess it will be solved if I do it like this: `Mail::to($user->email)->send(new NothingListenedFor3Days($user));` But I don't want to make a mistake how should this be spelled correctly. `Mail::to($email)->send(new MagicLink($auth, $link));` Any chance you could show me the correct spelling? – iguner Sep 02 '22 at 13:06
  • What kind of "spelling" are you searching for? Please add all clarification to your question by editing it – Nico Haase Sep 02 '22 at 13:07
  • @NicoHaase `Mail::to($email)->send(new MagicLink($auth, $link));` when I edit it this way, the problem should be resolved. => `Mail::to($user)` to `Mail::to($user->email)` Can you help me edit it this way? – iguner Sep 02 '22 at 14:27
  • "Can you help me edit it this way" - if you think that this resolves the problem, why not open your editor, make the change, and test it? What exactly should I (or anybody else) help you with? – Nico Haase Sep 02 '22 at 14:43

0 Answers0