Gravatar Error: Please specify a valid email address

570 views Asked by At

I am trying to use a gravatar in my application.

I have a collection of employees. Not all have a photo, therefore I check if the avatar_type === gravatar in order pass a valid URL of the email if exist therefore I do a loop:

foreach ($employees as $employee) {
  if($employee->avatar_type === 'gravatar') { 
      $employee->avatar_location = gravatar()->get($employee->email, ['size'=>80]);
   }
}

The first Item of the collection thet does not have a photo and needs a gravatar looks like this:

App\Models\Employee\Employee {#2898 ▼
      ...
      #attributes: array:14 [▼
        "firstname" => "Mary"
        "lastname" => "Brown"
        "email" => "[email protected]"
        "avatar_location" => null
        "avatar_type" => "gravatar" // <- Here is the flag
      ]

However, even if the email is valid ("email" => "[email protected]") I get the following error:

Creativeorange\Gravatar\Exceptions\InvalidEmailException
Please specify a valid email address

I even made a test and "dd" the email inside the loop and the email is there. Like this:

foreach ($employees as $employee) { 

 dd($employee->email)  //<- The email is here

  if($employee->avatar_type === 'gravatar') { 
      $employee->avatar_location = gravatar()->get($employee->email, ['size'=>80]);
   }
}

What am I missing here. Why is not a valid email?

I am using Laravel 6.13

The gravatar package is downloaded, installed and working in other places of the app (in the user profile page).

1

There are 1 answers

0
KPK On

You need to sanitize all the inputs before proceeding. In laravel we can write a simple middleware which performs this task on the fly. Here is an article which explains clearly. check this medium article Hope this helps.