簡體   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