可以通过以下步骤实现:
npm install sendgrid @sendgrid/mail
const fs = require('fs');
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey('YOUR_SENDGRID_API_KEY');
const senderEmail = 'sender@example.com';
const recipientEmail = 'recipient@example.com';
const logFilePath = 'path/to/log/file.log';
const logFileContent = fs.readFileSync(logFilePath, 'utf8');
const email = {
to: recipientEmail,
from: senderEmail,
subject: 'Log File',
text: 'Please find the attached log file.',
attachments: [
{
content: logFileContent,
filename: 'log_file.log',
type: 'text/plain',
disposition: 'attachment',
},
],
};
sgMail.send(email)
.then(() => {
console.log('Email sent successfully');
})
.catch((error) => {
console.error('Error sending email:', error);
});
以上代码将日志文件作为附件添加到电子邮件中,并使用sendgrid发送邮件。请确保将YOUR_SENDGRID_API_KEY
替换为您自己的sendgrid API密钥。
这种方法适用于需要将日志文件通过电子邮件发送给指定收件人的场景。sendgrid是一种云计算服务,提供可靠的电子邮件传递和管理解决方案。
领取专属 10元无门槛券
手把手带您无忧上云