106 lines
3.8 KiB
JavaScript
106 lines
3.8 KiB
JavaScript
const fs = require('fs');
|
||
const axios = require('axios');
|
||
const path = require('path');
|
||
const simpleParser = require('mailparser').simpleParser;
|
||
|
||
/*
|
||
指定したフォルダに有るemlファイルを1つずつ分析する
|
||
メッセージ項目と添付ファイルを取得し、プリザンターにアップロードする
|
||
|
||
*/
|
||
// emlファイルの保管先と完了ファイル移動先を固定変数で指定
|
||
const EML_SOURCE_DIR = './eml_files'; // ここを任意のパスに変更可
|
||
const EML_DONE_DIR = './eml_done'; // ここを任意のパスに変更可
|
||
|
||
// 必要なら移動先フォルダを作成
|
||
if (!fs.existsSync(EML_DONE_DIR)) {
|
||
fs.mkdirSync(EML_DONE_DIR, { recursive: true });
|
||
}
|
||
|
||
const folderPath = EML_SOURCE_DIR;
|
||
|
||
async function processEmlFiles() {
|
||
try {
|
||
const files = fs.readdirSync(folderPath).filter(file => file.endsWith('.eml'));
|
||
|
||
for (const file of files) {
|
||
const filePath = path.join(folderPath, file);
|
||
const parsed = await simpleParser(fs.createReadStream(filePath));
|
||
|
||
const messageData = {
|
||
from: parsed.from?.text,
|
||
to: parsed.to?.text,
|
||
subject: parsed.subject,
|
||
text: parsed.text,
|
||
html: parsed.html,
|
||
date: parsed.date
|
||
};
|
||
|
||
console.log(`Processing: ${file}`);
|
||
console.log('Message:', messageData);
|
||
|
||
// Handle attachments
|
||
if (parsed.attachments?.length > 0) {
|
||
for (const attachment of parsed.attachments) {
|
||
console.log(`Attachment: ${attachment.filename}`);
|
||
}
|
||
}
|
||
|
||
// Upload to Pleasanter
|
||
if (parsed.attachments?.length > 0) {
|
||
const firstAttachment = parsed.attachments[0];
|
||
const base64Data = firstAttachment.content.toString('base64');
|
||
await uploadToPleasanter(messageData, firstAttachment.filename, base64Data);
|
||
}
|
||
|
||
// 取り込み完了したemlファイルを移動(ファイル名の先頭に【済】を付与、重複時は連番)
|
||
const ext = path.extname(file);
|
||
const base = path.basename(file, ext);
|
||
let doneName = `【済】${base}${ext}`;
|
||
let destPath = path.join(EML_DONE_DIR, doneName);
|
||
let count = 1;
|
||
while (fs.existsSync(destPath)) {
|
||
doneName = `【済】${base}(${count})${ext}`;
|
||
destPath = path.join(EML_DONE_DIR, doneName);
|
||
count++;
|
||
}
|
||
fs.renameSync(filePath, destPath);
|
||
console.log(`Moved ${file} to ${destPath}`);
|
||
}
|
||
} catch (error) {
|
||
console.error('Error processing EML files:', error);
|
||
}
|
||
}
|
||
|
||
async function uploadToPleasanter(messageData, filename, base64Content) {
|
||
const apiKey = '6504c8a807677a3a576e10327f3c19876c55736ee45d4a845796b9e7f5e087bfd4bd0d8184863da8cf1733ca635111432ab334aea59102b06a96ee6d2c05190d';
|
||
const siteId = '451995'; // ←ご自身のサイトIDに変更してください
|
||
const url = `https://nextoffice.next-hd.co.jp/pleasanter/api/items/${siteId}/create`;
|
||
|
||
const lines = messageData.text.split('\n');
|
||
const extractedLines = lines.slice(6, 9).join('\n');
|
||
|
||
const uploadData = {
|
||
"ApiVersion": 1.1,
|
||
"ApiKey": apiKey,
|
||
"Body": extractedLines,
|
||
"AttachmentsHash": {
|
||
"AttachmentsA": [
|
||
{
|
||
"Name": filename,
|
||
"Base64": base64Content
|
||
}
|
||
]
|
||
},
|
||
};
|
||
|
||
try {
|
||
const response = await axios.post(url, uploadData);
|
||
|
||
console.log('Pleasanter登録結果:', response.data);
|
||
} catch (error) {
|
||
console.error('Pleasanter登録エラー:', error.response?.data || error.message);
|
||
}
|
||
}
|
||
|
||
processEmlFiles(); |