【问题标题】:How to add email attachment to strapi provider email nodemailer如何将电子邮件附件添加到 Strapi 提供商电子邮件 nodemailer
【发布时间】:2019-07-25 20:17:51
【问题描述】:

我正在尝试使用strapi-provider-email-nodemailer 发送一封带有附件的确认电子邮件。插件clearly supports attachments,但我不知道我想在strapi后端的哪个位置添加这些附件。有谁知道如何在通过strapi-nodemailer 发送的电子邮件中添加附件?

【问题讨论】:

    标签: email-attachments nodemailer strapi


    【解决方案1】:

    最终解决方案相当复杂。使用 Strapi 可能有一种更简单、未记录的方法,但我不知道。

    最后,我不得不根据快速入门指南创建一个全新的 API,因此创建一个新的内容类型“注册”并创建我自己的使用 strapi.plugins['email']strapi.plugins[users-permissions] 的自定义控制器和服务。

    Strapi 会自动为您生成模型、文档和路由,但您必须编辑控制器和服务。

    注册/配置/routes.json

    {
          "method": "POST",
          "path": "/registers",
          "handler": "Register.create",
          "config": {
            "policies": [],
            "description": "Register a user"
          }
        },
    

    注册/控制器/Register.js

    module.exports = {
      create: async ctx => {
        try {
          let addUser = await strapi.services.register.registerUser(
            ctx.request.body
          );
          let sendEmail = await strapi.services.register.sendEmail(
            ctx.request.body
          );
          ctx.send({
            user: addUser,
            email: sendEmail
          });
        } catch (err) {
          console.log(err);
        }
      }
    };
    
    

    register/services/Register.js(确保公用文件夹中有图片)

    'use strict';
    const path = require('path');
    
    module.exports = {
      registerUser: async data => {
        let result = await strapi.plugins['users-permissions'].services.user.add({
          username: data.username,
          email: data.email,
          password: data.password
        });
        return result;
      },
      sendEmail: async data => {
        console.log(__dirname);
        let result = await strapi.plugins['email'].services.email.send({
          to: data.email,
          from: 'whatever@whatever.com',
          replyTo: 'whatever@whatever.com',
          subject: 'Use strapi email provider successfully',
          text: 'Hello world foo!',
          html: 'Embedded image: <img src="cid:my_logo@nodemailer.com"/>',
          attachments: [
            {
              filename: 'surmile_logo.PNG',
              path: path.join(
                __dirname + '/../../../public/uploads/my_logo.png'
              ),
              cid: 'my_logo@nodemailer.com'
            }
          ]
        });
        return result;
      }
    };
    
    

    然后strapi buildstrapi start 发帖到http://localhost:1337/registers 带有 json 正文

    {
      "username": "sausage",
      "email": "blah@gmail.com",
      "password": "123456"
    }
    

    确保您在管理面板中授予所有人访问路由的权限

    确保your client is properly configured

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-20
      • 1970-01-01
      • 2016-06-13
      • 1970-01-01
      相关资源
      最近更新 更多