如何填充快速车把页面并将其作为电子邮件发送

问题描述 投票:0回答:1

我正在使用node.js、express、express-handlebars 和nodemailer。 我想填充 HBS 模板,然后将其发送到电子邮件,作为样式电子邮件...... 结果如下(首先在浏览器中呈现):

这是一个非常简单的页面,我在其中插入了两个图像(作为 64 基)和一些文本(如下)..

我使用的设置:

app.use(express.json());
app.set('view engine', 'hbs');
app.set('views', path.join(__dirname, 'views'));
app.use('/public', express.static(path.join(__dirname, 'public')));

现在,在控制器内部,我只是使用一些虚拟数据来显示和渲染视图。我还使用了一些 FS 函数将图像转换为字符串,稍后我将对其进行重构(目前功能是我的目标)。


import { NextFunction, Request, Response } from "express";
import { CustomErrorRouteHandler } from "../../errors/CustomErrorRoute";
import * as fs from 'fs';
import path = require("path");
import { sendEmailHTML } from "../../helpers/sendBasicEmials/nodemailer";
import * as expressHandlebars from 'express-handlebars';
import { compile } from 'handlebars';


export const registerClientInvitation = async (req: Request, res: Response, next: NextFunction) => {
    try {
        const { clientName, clientEmail, content, invitationLink } = req.body;

        // get the path and stringfy the image to base64
        let imagePath = path.join(__dirname, '..', '..', '/public', '/images', 'main-icon-email.png');
        let logoImage = await fs.promises.readFile(imagePath);
        let string64ImageBase = Buffer.from(logoImage).toString('base64');
        
        // get the path and stringfy the image to base64
        let imageDemoEmail = path.join(__dirname, '..', '..', '/public', '/images', 'account-email.jpg');
        let imageDemo = await fs.promises.readFile(imageDemoEmail);
        let string64ImageDemoBase = Buffer.from(imageDemo).toString('base64');        


        // get the htmlTemplate path and make it a string
        let hbsTemplatePAth = path.join(__dirname, '..', 'views', 'emails', 'invitaion.hbs');
        // let compiled = compile(hbsTemplatePAth, {data})
        

        res.render('emails/invitation', { 
            title:              'Account invitation',
            clientEmail:        clientEmail, 
            clientName:         'Daniel Daniel', 
            content:            'Welcome! We extend our warm invitation for you to create an account on our platform. Your participation in this step is vital as it facilitates the process of document review and signature. By creating an account, you gain access to our platform where you can seamlessly review and sign your documents. Thank you for choosing our platform; we look forward to your participation!',
            invitationLink:     invitationLink,
            logo:               string64ImageBase,
            additionalImage:    string64ImageDemoBase
        });   
    } 
    catch (error) {
        console.log(error);
        next(new CustomErrorRouteHandler('Error while creating an invitation', 500, 'failed'));
    }
}

我不想使用渲染方法,而是将其渲染到屏幕上,而是将其发送到电子邮件...... 我正在考虑使用 puppeteer 在后端进行渲染,创建 PDF 并像这样发送它,但这将是一种矫枉过正,不会达到我的目标。

我该怎么做? 非常感谢,丹尼尔

javascript node.js handlebars.js nodemailer
1个回答
0
投票

由于您在代码中导入了

sendEmailHTML
,我将假设此方法发送的电子邮件中包含 HTML 字符串。

要获取电子邮件的 HTML 内容,您可以使用 response.render 方法的第三个参数,该方法从车把模板接收生成的 HTML:

res.render(
    'emails/invitation',
    { 
        title: 'Account invitation',
        clientEmail: clientEmail, 
        clientName: 'Daniel Daniel', 
        content: 'Welcome! We extend our warm invitation for you to create an account on our platform. Your participation in this step is vital as it facilitates the process of document review and signature. By creating an account, you gain access to our platform where you can seamlessly review and sign your documents. Thank you for choosing our platform; we look forward to your participation!',
        invitationLink: invitationLink,
        logo: string64ImageBase,
        additionalImage: string64ImageDemoBase
    },
    (error, html) => {
        if (error) {
            // Treat here any error related with the html generation.
        } else {
            // Send email otherwise
            sendEmailHTML(clientEmail, html);
        }
    }
);   
© www.soinside.com 2019 - 2024. All rights reserved.