电子邮件 js 中的 STMP 客户端无法在 aws amplify 上运行

发布于 2025-01-14 08:02:01 字数 1313 浏览 3 评论 0原文

我正在尝试为我的网站上的用户建立一个电子邮件系统。我正在使用 nextJS 并有一个 api 端点来发送电子邮件。为了发送电子邮件,我使用 emailJS 并使用自定义正文将电子邮件发送给自己。以下是我的 email.js 文件的代码:

 import { SMTPClient } from 'emailjs';  
 
 
export default function handler(req, res) {
 
 const {body, subject}=req.body;
 // console.log(process.env)

  
 const client = new SMTPClient({
   user: "[email protected]",
   password: "passward",
   host: 'smtp.gmail.com',
   ssl:true
 });
 
 try{
 
  client.send(
     {
       text: `${body}`,
       from: "[email protected]",
       to: "[email protected]",
        subject: `${subject}`,
      
     }
     )
   }
 catch (e) {
     res.status(400).end(JSON.stringify({ message: e.message }))
   return;
 } 
  
 res.status(200).end(JSON.stringify({ message:'Mail sending' }))
}

当我在本地主机上使用该代码时,该代码可以工作,但当我部署到放大时,该代码不起作用。当我尝试在 amplify 上发出发布请求时,我收到状态 200 和 {"message":"Mail moving"}。但是,gmail 帐户永远不会收到电子邮件。我没有收到错误消息。我没有启用两步验证,并且允许安全性较低的应用程序,但仍然没有发送电子邮件。我真的很感激任何帮助。

I am trying to set up an emailing system for users on my website. I am using nextJS and have an api endpoint to send emails. To send the emails I am using emailJS and sending the email to myself with a custom body. Here is the code for my email.js file:

 import { SMTPClient } from 'emailjs';  
 
 
export default function handler(req, res) {
 
 const {body, subject}=req.body;
 // console.log(process.env)

  
 const client = new SMTPClient({
   user: "[email protected]",
   password: "passward",
   host: 'smtp.gmail.com',
   ssl:true
 });
 
 try{
 
  client.send(
     {
       text: `${body}`,
       from: "[email protected]",
       to: "[email protected]",
        subject: `${subject}`,
      
     }
     )
   }
 catch (e) {
     res.status(400).end(JSON.stringify({ message: e.message }))
   return;
 } 
  
 res.status(200).end(JSON.stringify({ message:'Mail sending' }))
}

The code works when I use it on localhost but it does not work when I deploy to amplify. When I try to make a post request on amplify I get status 200 with the {"message":"Mail sending"}. However, the gmail account never gets the email. I do not get an error message. I do not have 2 step verification on and have allowed less secure apps, but still no emails are being sent. I would really appreciate any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少女情怀诗 2025-01-21 08:02:01

emailjs 库利用排队系统来发送电子邮件。这意味着 send 方法会将电子邮件添加到队列中并稍后发送。在 lambda 函数中使用 send 方法时,这可能会导致问题,因为该函数可能会在电子邮件发送之前关闭。为了确保在 lambda 函数关闭之前发送电子邮件,您可以改用 sendAsync 方法。此方法返回一个承诺,该承诺将在电子邮件成功发送后得到解决。

要使用 sendAsync 方法发送电子邮件,您可以执行以下操作:

await client.sendAsync(
     {
       text: `${body}`,
       from: "[email protected]",
       to: "[email protected]",
       subject: `${subject}`,
     }
)

The emailjs library utilizes a queuing system for sending emails. This means that the send method adds the email to the queue and sends it at a later time. This can cause issues when using the send method within a lambda function, as the function may close before the email has been sent. To ensure that the email is sent before the lambda function closes, you can use the sendAsync method instead. This method returns a promise that will be resolved when the email has been successfully sent.

To send an email using the sendAsync method, you can do the following:

await client.sendAsync(
     {
       text: `${body}`,
       from: "[email protected]",
       to: "[email protected]",
       subject: `${subject}`,
     }
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文