带有用于电子邮件的自定义 Sendgrid 控制器代码的 Strapi Beta [英] Strapi Beta with custom Sendgrid Controller code for email

查看:32
本文介绍了带有用于电子邮件的自定义 Sendgrid 控制器代码的 Strapi Beta的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The structure for Strapi beta has changed how plugins are architected, removing the /plugins directory and the plugins are now kept in the /node_modules directory. I am trying to write some custom code to fire a confirmation email after an order is placed. In the previous version of Strapi, the email plugin directory was here:

/server/plugins/email/controllers

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

In this directory, the following code was wrote that is working in alpha in the SEND controller:

我们对此进行了注释://await.strapi.plugins.email.services.send(options, config);

然后此代码用于曾经存在的电子邮件控制器的 module.exports 内的 SEND 控制器...

And then this code is used in the SEND controller inside module.exports of the once extistent email 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({});

好的,那是服务器端的 Strapi Send 控制器……现在在客户端,在订单承诺返回后,我们触发另一个承诺,用于发送命中 Strapi API 的确认电子邮件:

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.我在 Strapi 中设置了带有 API 密钥、权限和权限的 SendGrid,一切都很好,问题是既然 Beta 架构已从 alpha 更改为合适的位置放置此代码?

In a nutshell, the question is now that the architecture of Strapi has changed, not sure where to put my server code above, and also the API url to call to fire the email. I have SendGrid setup in Strapi with API key, permissions, and everything is good to go, just a question of where is the proper place to put this code now that the Beta architecture has changed from alpha?

* 更新代码 *

根据 Jim 的建议,我现在在/extensions 文件夹中创建了一个电子邮件控制器,如下所示:

From Jim's suggestion, I have now created an Email controller in the /extensions folder like this:

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

电子邮件.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 来调用我们的电子邮件扩展,如下所示:

Now, in the client I use a promise with React to call our email extension like this:

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>'
        }
      });

它有效!电子邮件是从 Strapi 发送的,我看到控制器中的信息是电子邮件的一部分.问题?

It works! The email is sent from Strapi, and I see that the information in the controller is part of the email. The problem?

客户端中的 promise 是 try/catch 的一部分,并且在触发电子邮件后返回 404,因为它找不到 /email 作为路由.所以,我不明白为什么它可以工作并找到电子邮件的扩展控制器,触发电子邮件但返回 404 路由.

The promise in the client is part of a try/catch and after the email is fired it returns a 404, because it can't find /email as a route. So, I don't understand why it is working and finding the extension controller for email, firing the email but returning a 404 for the route.

我错误地从客户端调用了扩展控制器.

I am calling the extension controller from the client incorrectly.

推荐答案

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

Now you will have to follow the customization documentation. Here is the doc https://strapi.io/documentation/3.0.0-beta.x/concepts/customization.html#plugin-extensions

  • 您必须按照要更新的文件路径创建文件.
  • 然后你只复制你需要的函数.
  • 最后,您添加电子邮件服务电话.

这篇关于带有用于电子邮件的自定义 Sendgrid 控制器代码的 Strapi Beta的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆