繁体   English   中英

Strapi Beta 与自定义 Sendgrid Controller 代码为 email

[英]Strapi Beta with custom Sendgrid Controller code for email

Strapi beta 的结构改变了插件的架构方式,删除了 /plugins 目录,插件现在保存在 /node_modules 目录中。 我正在尝试编写一些自定义代码以在下订单后触发确认 email。 在之前版本的 Strapi 中,email 插件目录在这里:

/server/plugins/email/controllers

在此目录中,编写了以下代码,该代码在 SEND controller 的 alpha 版本中运行:

我们将其注释掉: // await.strapi.plugins.email.services.send(options, config);

然后此代码用于曾经存在的 email controller 的模块中的 SEND controller 内的导出...

let options = ctx.request.body;

   try {
   // send email to user
   await strapi.plugins['email'].services.email.send({
      to: options.to,
      from: 'test@example.com',
      subject: options.subject,
      text: options.text,
      html: options.html
   })
} catch (err) {
   return ctx.badRequest(null, err);
}

   // Send 200 'ok;
   ctx.send({});

Ok, that was the Strapi Send controller on the server side... now on the client after the promise for the order returns, we fire another promise for confirmation email that hits Strapi API:

await.strapi.request('POST', '/email', { 
   data: {
      to: confirmationEmailAdress,
      subject: "Order Confirmation',
      text: 'Your order has been processed',
      html: '<b>Expect your stuff to arrive broken. Thanks.</b>'
   }
});

简而言之,现在的问题是 Strapi 的架构发生了变化,不确定将我的服务器代码放在哪里,还有 API url 调用以触发 Z0C83F57C786A0B4A39EFAB23EBCZ31。 我在 Strapi 中设置了 SendGrid,使用 API 密钥、权限和对 go 的一切都很好,只是现在 Beta 架构已从 alpha 更改后,将这段代码放在哪里合适的问题?

*更新的代码*

根据 Jim 的建议,我现在在 /extensions 文件夹中创建了一个 Email controller,如下所示:

/server/extensions/email/controllers/Email.js

Email.js

'use strict';

module.exports = {

  send: async (ctx) => {
    let options = ctx.request.body;

    try {
      //Send email to the user
      await strapi.plugins['email'].services.email.send({
        to: options.to,
        from: 'test@example.com',
        subject: options.subject,
        text: options.text,
        html: options.html
      });
    } catch (err) {
      return ctx.badRequest(null, err);
    }
  }
}

现在,在客户端中,我使用带有 React 的 promise 来调用我们的 email 扩展,如下所示:

await strapi.request('POST', '/email', {
        data: {
          to: confirmationEmailAddress,
          subject: `Order Confirmation - South of Sleep ${new Date(Date.now())}`,
          text: 'Your order has been processed',
          html: '<bold>Except your stuff to be broken upon arriving</bold>'
        }
      });

它有效,email 是从 Strapi 发送的。 我看到 controller 中的信息是 email 的一部分? 问题?

客户端中的 promise 是 try/catch 的一部分,在 email 被触发后,它返回 404,因为它找不到/email作为路由。 所以,我不明白为什么它正在工作并为 email 找到扩展 controller,触发 email 但为路线返回 404。

我从客户端错误地调用了扩展 controller。

现在您必须遵循自定义文档。 这是文档https://strapi.io/documentation/3.0.0-beta.x/concepts/customization.html#plugin-extensions

  • 您必须创建遵循要更新的文件路径的文件。
  • 然后你只复制你需要的 function。
  • 最后,您添加您的 email 服务电话。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM