【问题标题】:Sending email via SendGrid after form submission提交表单后通过 SendGrid 发送电子邮件
【发布时间】:2022-01-11 01:06:43
【问题描述】:

我正在使用 Svelte 和 SendGrid 制作一个联系表单。这是一个基本的 app.svelte:

<script>
    import sgMail from '@sendgrid/mail';
    sgMail.setApiKey(import.meta.env.VITE_SENDGRID);

    function submitForm() {
        const msg = {
            to: 'test@example.com',
            from: 'test@example.com',
            subject: 'Sending with SendGrid is Fun',
            text: 'and easy to do anywhere, even with Node.js',
            html: '<strong>and easy to do anywhere, even with Node.js</strong>'
        };
        console.log('Form submitted');
        sgMail.send(msg);
    }
</script>

<form on:submit|preventDefault={submitForm}>
    <button type="submit">Submit</button>
</form>

尽管调用了该函数(它在控制台中记录Form submitted),但用户在表单上选择submit 后,上面的代码不会发送电子邮件。当我将所有代码从 submitForm() 移到函数外时,代码会在页面加载时执行,所以我知道这不是我的 API 密钥的问题。

有什么我缺少的建议吗?

【问题讨论】:

  • 尝试将sgMail.send(msg) 移到上一行,看看它是否再次记录成功消息。而且,控制台中是否显示任何错误?
  • @Kalyan 当我移动sgMail.send(msg); 时,似乎没有记录Form submitted。如果有帮助,我注意到当我将const msg = { .. } 移到函数之外时,我能够让控制台记录Form submitted
  • @R Barnes 我对发送网格 API 不太熟悉,但似乎 sgMail.send() 没有被执行或抛出错误。之前,当您将“console.log”放在send(msg) 上方时,即使未调用send(),也会记录该消息。
  • sgMail.send(msg).then(() =&gt; {}, error =&gt; { console.error(error); if (error.response) { console.error(error.response.body) }}); 试试这个,以便记录来自 sendmail api 的任何错误。

标签: sendgrid svelte sveltekit


【解决方案1】:

Svelte 只是一个前端环境。 Sendgrid 包是为服务器端/node.js 环境设计的。在您的示例中,您的 Sendgrid API 密钥将被公开,因为您尝试在前端/客户端使用它。

一个解决方案可能是查看 SvelteKit,它具有始终在服务器端运行的“端点”概念。或者您可以创建一个快速服务器来处理向 Sendgrid 发送电子邮件。

编辑:解决方案是使用 Sveltekit 端点。端点始终在服务器上运行。您的最终解决方案可能如下所示:

文件:/src/routes/api/sendmail.ts 或 /src/api/sendmail.js

import sgMail from "@sendgrid/mail";
sgMail.setApiKey(import.meta.env.VITE_SENDGRID);

export async function get(page) {
      const msg = {
        to: "test@example.com",
        from: "test@example.com",
        subject: "Sending with SendGrid is Fun",
        text: "and easy to do anywhere, even with Node.js",
        html: "<strong>and easy to do anywhere, even with Node.js</strong>",
      };
      console.log("Form submitted");
      const output = await sgMail.send(msg);
  return {
    body: output,
  };
}

文件 /src/routes/index.svelte

<script>
  function submitForm() {
    fetch("/api/sendmail");
  }
</script>

<form on:submit|preventDefault={submitForm}>
  <button type="submit">Submit</button>
</form>

【讨论】:

  • 这是有道理的,看起来这是一个 CORS 问题——有趣的是 Safari 控制台没有提到这一点,但 Chrome 却提到了。我目前正在使用 SvelteKit,你知道有什么资源可以分享如何做到这一点吗?
  • 我正忙着写一个基于 SvelteKit 的解决方案。一瞬间
  • 非常感谢您对此提供的帮助。我从您的示例运行代码并将脚本放在/src/api/sendmail.js 但控制台显示GET http://localhost:3000/api/sendmail 404 (Not Found)。您是否知道可能导致此问题的原因?
  • 如果您在浏览器中直接浏览该路线,您应该能够访问该路线。 localhost:3000/api/sendmail。那样有用吗?你用的是什么 SvelteKit 适配器?
  • 我收到了Not found: /api/sendmail,特别是在解析、processTicksAndRejections、异步 Object.handle、异步响应和异步文件中。我使用的是适配器自动然后移动到适配器节点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-28
  • 2016-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多