我具有发送带有pdf附件的电子邮件的功能,但是当我尝试发送本地没有文件的pdf时,电子邮件到达时没有任何附件...
async sendEmailWithAtt(email, subject, message, pathAtt) {
const transporter = nodemailer.createTransport({
host: process.env.EMAIL_HOST,
port: process.env.EMAIL_PORT,
auth: {
user: process.env.EMAIL_USERNAME,
pass: process.env.EMAIL_PASSWORD
},
attachments: [
{
filename: 'Document',
path: pathAtt, // URL of document save in the cloud.
contentType: 'application/pdf'
}
]
});
const mailOptions = {
from: process.env.EMAIL_USERNAME,
to: email,
subject: subject,
text: message
};
await transporter.sendMail(mailOptions);
}
答案 0 :(得分:1)
Nodemailer documentation指出,如果要使用URL,则必须使用href
代替为本地文件保留的path
。
如果该文件不是公开可用的,则可以使用httpHeaders
属性传递一些标题。
attachments: [
{
filename: 'Document',
href: pathAtt, // URL of document save in the cloud.
contentType: 'application/pdf'
}
]
除此之外,您还需要在attachments
中设置.createTransport
,而应将其放在消息中。
const mailOptions = {
from: process.env.EMAIL_USERNAME,
to: email,
subject: subject,
text: message,
attachments: [ /* ... */]
};
await transporter.sendMail(mailOptions);
答案 1 :(得分:-1)
function sendFile(){
let transporter = nodemailer.createTransport({
host: '',//put email_host
port: '', // put port
secure: true,
auth: {
user: '',//put user email
pass: '' //put password
}
});
let mailToUser = {
from: 'demo@gmail.com', //replace with user_email
to: 'test@gmail.com', // replace with that want to send it
html: "Hello",
attachments: [
{
filename: `test.pdf`
path :`` //put S3 url
}
]
};
transporter.sendMail(mailToUser, (error, info) => {
if (error) {
console.log(error);
}
console.log('Message sent: %s', info);
});
}