Next.js Discord

Discord Forum

Sending SMTP and Tg chat from Vercel

Answered
Mugger Crocodile posted this in #help-forum
Open in Discord
Mugger CrocodileOP
Hi there!
I'm facing issues upon deploying my Next app to Vercel.

I set up my app so it sends an email with an attachment with nodemailer and a telegram bot message. This is for distributing a pdf book along with tg admin updates so the team knows how many and to whom where sent.

It all works like a charm in localhost but doesn't from the vercel platform. I did some logging and they seems to show that both nodemailer's transporter.sendMail(mailOptions) and telegram's bot.sendMessage(chatId, response) lag on forever.

My code is set up like this:

reandar/src/app/mp/route.ts:
import bot from '@/app/lib/tg'
import { emailer } from '../lib/mailer'
import { NextResponse } from 'next/server'

// I'm using DELETE to test
export async function DELETE(req: Request){
  await emailer.sendBook('my.email@gmail.com', 'Vlad')
  await bot.sendMessage(process.env.TG_CHAT_ID!, `Hi there`)
  return NextResponse.json({ok: true})
}


Then the imported tg bot:
import TelegramBot from 'node-telegram-bot-api'
const token = process.env.TG_BOT_TOKEN;
if(!token) throw new Error("Tg token missing")
const bot = new TelegramBot(token, {polling: true});
export default bot


And the emailer:
import * as nodemailer from "nodemailer";
import { MailOptions } from "nodemailer/lib/json-transport";
import path from "path";

export class Emailer {
  private readonly transporter: nodemailer.Transporter;
  constructor() {
    this.transporter = nodemailer.createTransport({
      service: "gmail",
      host: 'smtp.gmail.com',
      port: 465,
      secure: true,
      auth: {
        user: process.env.GMAIL_USER,
        pass: process.env.GMAIL_PASSWORD,
      },
    });
  }

  public async sendBook(email: string, nombre: string) {
    const mailResponse = this.transporter.sendMail(mailOptions);
    console.log(`mailResponse:`)
    console.log(mailResponse)
  }
}

export const emailer = new Emailer();

I'm sure env is correct and I never see mailresponse in the log
Answered by riský
/is this.transporter.sendMail( async or not? (returns a promise?)
View full answer

5 Replies

Mugger CrocodileOP
I have a debugging statement just before the const mailResponse = this.transporter.sendMail(mailOptions); line, which I see in the logs, and then another afterwards, the console.log('mailResponse:') line, which I never see. This points to the flow lagging in there. If I swap the sendBook and sendMessage lines I get the same for the tg bot... I see the log line before calling .sendMessage but not those afterwards
thats weird because vercel says that it doesn't block smtp: https://vercel.com/guides/serverless-functions-and-smtp, but you probably should consider using one of the apis listed for better support with serverless...
If you wish to use SMTP rather than a third-party mail service, it is essential that you await the sending of the message, this can take a few seconds depending on the provider and library
@Mugger Crocodile after some further think, you should await your sending of email
/is this.transporter.sendMail( async or not? (returns a promise?)
Answer
Mugger CrocodileOP
Thanks for the feedback! I managed to solve it! Indeed it was related to some missing await. It took me a while to figure it out 😅
The async maze...
Cheers! 🙌🏼