Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

problem sending email using sendgrid #235

Open
igorskiter opened this issue May 12, 2022 · 1 comment
Open

problem sending email using sendgrid #235

igorskiter opened this issue May 12, 2022 · 1 comment

Comments

@igorskiter
Copy link

when trying to send email by sendgrid the lambda does not show log and ends up giving timeout. I've tried putting it inside a promise, but it didn't help. The initial handler has already been placed asyncronously. The same error also happens when I try to upload on S3. This problem only happens when it is deployed on aws, on serverless-offline it works correctly.

handler.js

import app from './';
import serverless from 'serverless-http';

const handler = serverless(app);

module.exports.handler = async (event, context) => {
  // you can do other things here
  const result = await new Promise(async (resolve, reject) => {
    const resProm = await handler(event, context);
    resolve(resProm);
  });
  // and here
  return result;
};

serveless.yml

functions:
  app:
    name: app-${env:STAGE}
    handler: src/handler.handler
    events:
      - http: ANY /
      - http: 'ANY {proxy+}'

sendMail.js

'use strict';

import crypto from 'crypto';
import sgMail from '@sendgrid/mail';

const forgetPassword = async event => {
  try{
    const { email } = event.body;

    const token = crypto.randomBytes(3).toString('hex');
    
    const text  = `<p>Você esqueceu sua senha?</p>
    <p>Codigo de autorização para mudar senha: ${token}</p>
    <p>Se não pediu para recuperar a senha ignore esse e-amil.</p>`;
    
    const msg = {
      to: [<<EMAIL>>],
      from: 'noreplay@playspts.com.br',
      subject: 'Código para resetar senha',
      text,
      html: text
    }

    sgMail.setApiKey(process.env.SENDGRID_API_KEY);

    const sendMail = await sgMail.send(msg);

    console.log( sendMail);

    return {
      statusCode: 200,
      body: JSON.stringify(
        {
          message: 'Pedido de token enviado, verifique seu e-mail!',
          sendMail
        }
      ),
    }
  }catch(e){
      console.error(e, "forgetPassword EROOR");
  }
  return {
    statusCode: 400,
    error: true,
    body: JSON.stringify(
      {
        message: 'Falha na recuperação da senha!',
        input: event,
      }
    ),
  }
}

export default Auth;
@dougmoscrop
Copy link
Owner

const result = await new Promise(async (resolve, reject) => {
    const resProm = await handler(event, context);
    resolve(resProm);
  });

This is risky, the Promise constructor does not, as far as I know, respect rejected promises and will therefore never settle. It's posisble you have an unhandled rejection being swallowed; you should at least do:

try {
res = await ..
resolve(res)
} catch (e) {
reject(e)
}

but of course, you can see that this entire promise wrapper is not useful as you can just move the await up!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants