Strapi Beta with custom Sendgrid Controller code for email

拜拜、爱过 提交于 2019-12-22 18:08:00

问题


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

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

We comment this out: // await.strapi.plugins.email.services.send(options, config);

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({});

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

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?

* UPDATED CODE *

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

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

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

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

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.


回答1:


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

  • You will have to create the file that follow the file path you want to update.
  • Then you copy only the function that you need.
  • And finally you add your email service call.


来源:https://stackoverflow.com/questions/58560598/strapi-beta-with-custom-sendgrid-controller-code-for-email

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!