javascript 如何填充快速搜索栏页面并将其作为电子邮件发送

w51jfk4q  于 5个月前  发布在  Java
关注(0)|答案(1)|浏览(45)

我正在使用node.js,express,express-tubebars和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文件,并发送它一样,但这将是一个矫枉过正,不会达到我的目标。
我怎么能这样做呢?非常感谢,丹尼尔

pinkon5k

pinkon5k1#

因为在你的代码中,你导入了一个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);
        }

        // Other code (send response to client?)
    }
);

字符串

相关问题