Vercel 不发送电子邮件

Posted

技术标签:

【中文标题】Vercel 不发送电子邮件【英文标题】:Vercel not sending email 【发布时间】:2022-01-21 19:39:34 【问题描述】:

我使用 SendGrid,我知道我的 SendGrid-API 密钥正在通过从终端检查来工作。 我在 SendGrid 发件人身份中验证了我的电子邮件。

curl -i --request POST \
--url https://api.sendgrid.com/v3/mail/send \
--header 'Authorization: Bearer MY-API-KYE_HERE' \
--header 'Content-Type: application/json' \
--data '"personalizations": ["to": ["email": "myname@gmail.com"]],"from": "email": "connect@mywebsite.com","subject": "SendGrid Test!","content": ["type": "text/plain", "value": "Howdy!"]'
HTTP/1.1 202 Accepted
Server: nginx
Date: Mon, 20 Dec 2021 04:42:08 GMT
Content-Length: 0
Connection: keep-alive
X-Message-Id: 9G5w8P8_SJWPwj1acrNRPQ
Access-Control-Allow-Origin: https://sendgrid.api-docs.io
Access-Control-Allow-Methods: POST
Access-Control-Allow-Headers: Authorization, Content-Type, On-behalf-of, x-sg-elas-acl
Access-Control-Max-Age: 600
X-No-CORS-Reason: https://sendgrid.com/docs/Clas-s-room/Basics/API/cors.html
Strict-Transport-Security: max-age=600; includeSubDomains

我收到一封电子邮件。

它也适用于本地主机。

但是当我使用以下代码部署到 Vercel 时,它不会发送任何电子邮件。

import sgMail from '@sendgrid/mail'
import dotenv from 'dotenv'
dotenv.config()

const host = process.env['HOST_URL']
const email_from = process.env['EMAIL_FROM']
const SENDGRID_API_KEY = process.env['SENDGRID_API']

export const sendGridConfirmationEmail = async (name, email, confirmationCode) => 
  await sgMail.setApiKey(SENDGRID_API_KEY)
  const msg = 
    to: email,
    from: `$email_from`,
    subject: "Please confirm your account",
    text: `Email Confirmation: Hello $name.
    Please confirm your email by clicking on the following link.
    Click here, $host/auth/confirm/$confirmationCode`,
    html:`<h1>Email Confirmation</h1>
    <h2>Hello $name</h2>
    <p>Please confirm your email by clicking on the following link.</p>
    <a href=$host/auth/confirm/$confirmationCode> Click here</a>
    </div>`,
  
  await sgMail
    .send(msg)
    .then(() => 
      console.log('Email sent')
    )
    .catch((error) => 
      console.error(error)
    )

我也尝试了以下方法,但也没有用。


export const sendGridConfirmationEmail = async (name, email, confirmationCode) => 
   await fetch(SENDGRID_API, 
     method: 'POST',
     headers: 
       'Content-Type': 'application/json',
       Authorization: `Bearer $SENDGRID_API_KEY`
     ,
     body: JSON.stringify(
       personalizations: [
         
           to: [
             
               email
             
           ],
           subject: 'Demo success :)'
         
       ],
       from: 
         email: email_from,
         name: 'Test SendGrid'
       ,
       content: [
         
           type: 'text/html',
           value: `Congratulations <b>$name</b>, you just sent an email with sendGrid. $confirmationCode`
         
       ]
     )
 );

如何使用 SendGrid 从 Vercel 发送电子邮件?

【问题讨论】:

一个函数的 Vercel 超时为 5 秒,可能是它在完成执行之前被丢弃,因此无法发送电子邮件 【参考方案1】:

我解决了这个问题。

当我调用sendGridConfirmationEmail时,我需要使用await


export const post = async ( body ) => 
...

 if (mail_method === "sendgrid") 
    await sendGridConfirmationEmail(
      body.name,
      body.email,
      token
    );
      console.log('SendGrid registration is emailed.')
  
...

【讨论】:

以上是关于Vercel 不发送电子邮件的主要内容,如果未能解决你的问题,请参考以下文章

无法在 vercel 生产中验证 spotify

gatsby 无法获取所有关于 vercel 生产的 graphql 数据

PHP发送带有文件附件的电子邮件 - 电子邮件根本不发送

贝宝发送发票不发送电子邮件

javamail电子邮件不发送

发送的电子邮件客户端中不存在 Android 发送电子邮件