Supabase Edge Function Can't Send Email

145 Views Asked by At

I'm trying to send and email using SmtpClient from deno, and configuring it using gmail app password. strange thing is when i serve the function locally emails get sent but once deployed i get the following error in the function logs

TypeError: Deno.writeAll is not a function
    at BufWriter.flush (https://deno.land/[email protected]/io/bufio.ts:394:24)
    at SmtpClient.writeCmd (https://deno.land/x/[email protected]/smtp.ts:125:28)
    at eventLoopTick (ext:core/01_core.js:183:11)
    at async SmtpClient._connect (https://deno.land/x/[email protected]/smtp.ts:82:9)
    at async SmtpClient.connectTLS (https://deno.land/x/[email protected]/smtp.ts:39:9)
    at async Server.<anonymous> (file:///src/index.ts:847:9)
    at async Server.#respond (https://deno.land/[email protected]/http/server.ts:221:24)

i tried wrapping every dangerous operations in try catch block no errors return except the on i find in logs

1

There are 1 best solutions below

0
On

I had the same problem and found a solution here: https://deno.land/x/[email protected]

This is how to implement it

// supabase/functions/sendMail/index.ts

import { SMTPClient } from "https://deno.land/x/denomailer/mod.ts";

const smtp = new SMTPClient({
    connection: {
      hostname: "smtp.gmail.com",
      port: 465,
      tls: true,
      auth: {
        username: Deno.env.GMAIL_USER,
        password: Deno.env.GMAIL_PASSWORD,
      },
    },
  })

Deno.serve(async (req) => {
  try {
    await smtp.send({
      from: Deno.env.GMAIL_USER,
      to: req.to,
      subject: `Your Subject Line`,
      content: `Content that could be plain text or HTML`,
    })
  } catch (error) {
    return new Response(error.message, { status: 500 })
  }

  await smtp.close()

  return new Response(
    JSON.stringify({
      done: true,
    }),
    {
      headers: { 'Content-Type': 'application/json' },
    }
  )
})