';
+ const InvalidFileMessage = `
${i18n.t(
+ 'This_attachment_is_not_supported',
+ { lng: userLanguage },
+ )}
`;
+
+ for await (const message of messages) {
+ let author;
+ if (message.u._id === visitor._id) {
+ author = i18n.t('You', { lng: userLanguage });
+ } else {
+ author = showAgentInfo ? message.u.name || message.u.username : i18n.t('Agent', { lng: userLanguage });
+ }
+
+ const isSystemMessage = MessageTypes.isSystemMessage(message);
+ const messageType = isSystemMessage && MessageTypes.getType(message);
+
+ let messageContent = messageType
+ ? `
${i18n.t(
+ messageType.message,
+ messageType.data
+ ? { ...messageType.data(message), interpolation: { escapeValue: false } }
+ : { interpolation: { escapeValue: false } },
+ )}`
+ : message.msg;
+
+ let filesHTML = '';
+
+ if (message.attachments && message.attachments?.length > 0) {
+ messageContent = message.attachments[0].description || '';
+
+ for await (const attachment of message.attachments) {
+ if (!isFileAttachment(attachment)) {
+ // ignore other types of attachments
+ continue;
+ }
+
+ if (!isFileImageAttachment(attachment)) {
+ filesHTML += `
${attachment.title || ''}${InvalidFileMessage}
`;
+ continue;
+ }
+
+ if (!attachment.image_type || !acceptableImageMimeTypes.includes(attachment.image_type)) {
+ filesHTML += `
${attachment.title || ''}${InvalidFileMessage}
`;
+ continue;
+ }
+
+ // Image attachment can be rendered in email body
+ const file = message.files?.find((file) => file.name === attachment.title);
+
+ if (!file) {
+ filesHTML += `
${attachment.title || ''}${InvalidFileMessage}
`;
+ continue;
+ }
+
+ const uploadedFile = await Uploads.findOneById(file._id);
+
+ if (!uploadedFile) {
+ filesHTML += `
${file.name}${InvalidFileMessage}
`;
+ continue;
+ }
+
+ const uploadedFileBuffer = await FileUpload.getBuffer(uploadedFile);
+ filesHTML += `
${file.name}
`;
+ }
+ }
+
+ const datetime = moment.tz(message.ts, timezone).locale(userLanguage).format('LLL');
+ const singleMessage = `
+
${author} ${datetime}
+
${messageContent}
+
${filesHTML}
+ `;
+ html += singleMessage;
+ }
+
+ html = `${html}
`;
+
+ const fromEmail = settings.get