0

hi guys i'm having some trouble with my production application:: in a few words what it should do is sending an email using SendinBlue API everithing is fine when in local but the application begins to act weirdly in production when a request with an email-address is sent from my frontend this function should fire

public function checkEmail(Request $request)
    {
        $email = $request->input('email');

        // Controlla se l'email è presente nel database
        $emailRecord = Email::where('email', $email)->first();

        if ($emailRecord) {
            // Se l'email è già presente nel database e associata a un token, ritorna il token
            error_log('mail gia esistente');
            $token = $emailRecord->token()->first();
            error_log('token= '. $token->token);
            error_log('code= '. $code->promo_code);
            $code = $token->codes()->first();
            return $this->sendEmail($request, 285, $code->promo_code);

        } else {
            // Se l'email non è presente nel database, crea un nuovo token e associarlo all'email
            $tokenValue = Str::random(16);

            try {
                $token = new Token(['token' => $tokenValue]);
                $token->save();

                $newEmail = new Email(['email' => $email, 'token_id' => $token->id]);
                $newEmail->save();
                error_log('nuova mail aggiunta');
                return $this->sendEmail($request, 284);
            }catch (\Exception $e){
                error_log('errore nell aggiunta della nuova mail');
                return response()->json(['error' => 'Si è verificato un errore durante la creazione della email e del token'], 500);
            }
        }
    }

then this function is called to send the mail

public function sendEmail(Request $request, $templateId, $code = "")
    {
        error_log('invio mail...');

        $emailTo = $request->input('email');

        // Trova il token nel database in base all'email
        $token = Email::where('email', $emailTo)->first()->token;

        // Inserisci qui la tua chiave API di Sendinblue
        $apiKey = Env::get("API_KEY");

        // Configura l'API client di Sendinblue
        $config = Configuration::getDefaultConfiguration()->setApiKey('api-key', $apiKey);
        $apiInstance = new TransactionalEmailsApi(new Client(), $config);

        // Prepara i dati dell'email
        $redeemUrl = Env::get('APP_URL') . "/code?token=".$token->token;
        $htmlContent = '<a href="' . $redeemUrl . '">LINK AL CODICE SCONTO</a> TOKEN=' . $token->token;

        $emailData = new SendSmtpEmail([
            'templateId' => $templateId,
            'params' => [
                'LINK' => $redeemUrl,
                'CODE' => $code,
            ],
            'to' => [['email' => $emailTo]],
        ]);

        try {
            // Invia l'email utilizzando le API di Sendinblue
            $result = $apiInstance->sendTransacEmail($emailData);

            // Ritorna una risposta positiva al frontend (ad esempio, una risposta JSON vuota)
            return response()->json([], 200);
        } catch (\Exception $e) {
            error_log('errore nell invio della mail');
            // Gestisci eventuali errori nell'invio dell'email
            return response()->json(['error' => $e->getMessage()], 500);
        }
    }

What I'm receiving in the console when I submit an already saved email address is a generic 500 error with "Internal Server Error" message

looks like thet when in production the error_log('code='. $code->promo_code) is not showing up in the console any idea what the problem could be? or any idea on how should i debug the problems?

  • The 500 error should be accompanied by more detail in your log - by default, somewhere in `storage/logs` on your server. – ceejayoz Sep 01 '23 at 15:51
  • this is the error `[2023-09-01 15:58:46] localPP_DEBUG=true.ERROR: Attempt to read property "promo_code" on null` – Armando_pagano Sep 01 '23 at 16:00
  • That implies `$token->codes()->first()` is not returning a code. – ceejayoz Sep 01 '23 at 16:04
  • yes what your saying is true, but i can't figure out why in development the same code is working fine – Armando_pagano Sep 01 '23 at 16:06
  • Presumably the *data* is different. Track it down line-by-line; look up each row that you expect to find in the production database. Make sure you're looking at `deleted_at` columns, too; soft-deleted records often cause this sort of trouble. – ceejayoz Sep 01 '23 at 16:08
  • `error_log('code='. $code->promo_code)` - Sidenote, to log something to Laravel's logs, the syntax is `\Log::error(code='. $code->promo_code)` (you can omit the leading \ if you include `Illuminate\Support\Facades\Log;` at the top of your file) – Tim Lewis Sep 01 '23 at 19:00

0 Answers0