我是一名新开发人员,现在我在react项目中工作,我必须附加已经在客户端中通过使用react-pdf生成的pdf文件,电子邮件中将通过使用nodemailer发送。 我通过谷歌搜索,但找不到正确的解决方案,所以也许有人可以帮助我。
这是客户端生成pdf、样式pdf和服务器端nodemailer
这是客户端生成的pdf:
`
import ReactDOMServer from "react-dom/server";
import html2pdf from "html2pdf.js/dist/html2pdf.min";
import Pdf from "./Pdf";
function generatePDF(userProfile) {
const printElement = ReactDOMServer.renderToString(
Pdf({
profilepicture: userProfile.profilepicture,
firstname: userProfile.firstname,
lastname: userProfile.lastname,
designation: userProfile.designation,
email: userProfile.email,
phonenumber: userProfile.phonenumber,
summary: userProfile.summary,
experience:userProfile.experience,
education:userProfile.education,
projects:userProfile.projects,
technologies:userProfile.technologies
})
);
var opt = {
margin: [15, 0, 30, 0], //top, left, buttom, right,
enableLinks: true,
filename: "pdfreport",
html2canvas: { useCORS: true },
jsPDF: {},
};
html2pdf()
.from(printElement)
.set(opt)
.toPdf()
.get("pdf")
.then(function (pdf) {
pdf.setPage(1);
})
.save();
}
export default {
generatePDF,
};
and here is the nodemailer in the server side:
import nodemailer from "nodemailer";
const sendEmail = async (subject, message, send_to, sent_from, reply_to ) => {
const transporter = nodemailer.createTransport({
host: process.env.EMAIL_HOST,
port: "465",
secure: "true",
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASS,
},
});
const options = {
from: sent_from,
to: send_to,
replyTo: reply_to,
subject: subject,
html: message,
attachment: [{
filename: "pdfreport",
}]
};
// Send Email
transporter.sendMail(options, function (err, info) {
if (err) {
console.log(err);
} else {
console.log(info);
}
});
};
export default sendEmail;
`
nodemailer 文档解释了如何添加附件。在您的选项对象中,您当前有:
attachment: [{
filename: "pdfreport",
}]
您需要:
attachments: [{
filename: "pdfreport",
content: // string, buffer, stream, or file path
}]
您还没有弄清楚PDF是如何保存或发送到服务器的。如果使用 html2pdf.js 保存,则发生在客户端,如果您使用的是 nodemailer,则发生在服务器端。您需要确保您了解 pdf 数据如何到达服务器,然后您就会知道在 nodemailer 选项的“内容”属性中放置什么内容