I'm doing an API-REST with lumen, and this is trying to send an email in a method of a controller, I'm using PHPMailer, and the problem I have is that the email takes a long time to send and until it is sent API-REST does not return the state of the request, so I wanted to ask if I could do some form of sending the request to send the mail but that the request does not wait for the email to be sent, but instead it is sent in " background "so to speak
I attach the code that I currently have
/**
* Crea un usuario
* Crea el usuario siempre y cuando se pasen todos los parametros necesarios
* Comprueba que el usuario no existe ya, si existe no te deja crearlo
*
* @param Request $request request en la que van los datos necesarios para la creación del usuario
* @return $this success true si se ha creado, false si no lo ha echo
*/
public function crearUsuario(Request $request){
try {
//Parametros necesarios para poder crear el usuario
$parametrosNecesarios = [
'email',
'password'
];
$this->comprobarRequest($request, $parametrosNecesarios);
//Si la request esta correcta entonces procedemos a crear al usuario
$email = $request->input('email');
$existeUsuario = Usuario::where('email',$email)->first();
if ($existeUsuario)
throw new Exception('UsuarioExiste');
if (strlen($request->input('password')) < 6)
throw new Exception("LongitudPassword");
if (!$this->is_valid_email($email))
throw new Exception("EmailIncorrecto");
$rol = Rol::where('nombre','Ba')->first();
$datosUsuario = [
'email' => $email,
'password' => $request->input('password'),
'rol_id' => $rol->id
];
Usuario::create($datosUsuario);
//TODO: Notificar al email
/*$asunto = 'Registro Correcto';
$body = 'This is the HTML message body <b>in bold!</b>';
$altBody = 'This is the body in plain text for non-HTML mail clients';
$host = 'smtp.1and1.es';
$port = 587;
$this->sendEmail($email,'[email protected]','',$port,
$asunto,$body,$altBody,$host
);*/
} catch (QueryException $e) {
return response(["success" => false, "message" => $e->getMessage()], 400)
->header('Content-Type', 'application/json');
} catch (Exception $e) {
return response(["success" => false, "message" => $e->getMessage()], 200)
->header('Content-Type', 'application/json');
} catch (FatalThrowableError $e) {
return response(["success" => false, "message" => $e->getMessage()], 400)
->header('Content-Type', 'application/json');
}
//Si el usuario se crea correctamente devolvemos true
return response(["success" => true], 200)
->header('Content-Type', 'application/json');
}