|
| 1 | +<script setup lang="ts"> |
| 2 | +import PostalMime from 'postal-mime'; |
| 3 | +
|
| 4 | +const inputType = ref<'file' | 'content'>('file'); |
| 5 | +const emailContent = ref(''); |
| 6 | +const fileInput = ref() as Ref<File | null>; |
| 7 | +const error = ref(''); |
| 8 | +
|
| 9 | +const parsedEmail = computedAsync(async () => { |
| 10 | + const emailContentValue = emailContent.value; |
| 11 | + const file = fileInput.value; |
| 12 | + error.value = ''; |
| 13 | + try { |
| 14 | + if (inputType.value === 'file' && file) { |
| 15 | + return await PostalMime.parse(await file.arrayBuffer()); |
| 16 | + } |
| 17 | + else if (inputType.value === 'content' && emailContentValue) { |
| 18 | + return await PostalMime.parse(emailContentValue); |
| 19 | + } |
| 20 | + else { |
| 21 | + return null; |
| 22 | + } |
| 23 | + } |
| 24 | + catch (e: any) { |
| 25 | + error.value = e.toString(); |
| 26 | + return null; |
| 27 | + } |
| 28 | +}); |
| 29 | +
|
| 30 | +function downloadFile(data: ArrayBuffer, fileName: string, fileType: string) { |
| 31 | + const blob = new Blob([data], { type: fileType || 'application/octet-stream' }); |
| 32 | + const downloadUrl = URL.createObjectURL(blob); |
| 33 | + const a = document.createElement('a'); |
| 34 | + a.href = downloadUrl; |
| 35 | + a.download = fileName; |
| 36 | + document.body.appendChild(a); |
| 37 | + a.click(); |
| 38 | + URL.revokeObjectURL(downloadUrl); |
| 39 | +} |
| 40 | +
|
| 41 | +function onUpload(file: File) { |
| 42 | + if (file) { |
| 43 | + fileInput.value = file; |
| 44 | + } |
| 45 | +} |
| 46 | +</script> |
| 47 | + |
| 48 | +<template> |
| 49 | + <div style="max-width: 600px;"> |
| 50 | + <c-card title="Input" mb-2> |
| 51 | + <n-radio-group v-model:value="inputType" name="radiogroup" mb-2 flex justify-center> |
| 52 | + <n-space> |
| 53 | + <n-radio |
| 54 | + value="file" |
| 55 | + label="File" |
| 56 | + /> |
| 57 | + <n-radio |
| 58 | + value="content" |
| 59 | + label="Content" |
| 60 | + /> |
| 61 | + </n-space> |
| 62 | + </n-radio-group> |
| 63 | + |
| 64 | + <c-file-upload |
| 65 | + v-if="inputType === 'file'" |
| 66 | + title="Drag and drop EML file here, or click to select a file" |
| 67 | + @file-upload="onUpload" |
| 68 | + /> |
| 69 | + |
| 70 | + <c-input-text |
| 71 | + v-if="inputType === 'content'" |
| 72 | + v-model:value="emailContent" |
| 73 | + label="Email Content" |
| 74 | + multiline |
| 75 | + placeholder="Put your eml/email content here..." |
| 76 | + rows="15" |
| 77 | + mb-2 |
| 78 | + /> |
| 79 | + </c-card> |
| 80 | + |
| 81 | + <c-alert v-if="error"> |
| 82 | + {{ error }} |
| 83 | + </c-alert> |
| 84 | + |
| 85 | + <c-card v-if="!error && parsedEmail" title="Output"> |
| 86 | + <input-copyable v-if="fileInput?.name" label="File Name" :value="fileInput?.name" /> |
| 87 | + <input-copyable v-if="parsedEmail.date" label="Date" :value="parsedEmail.date" /> |
| 88 | + <input-copyable v-if="parsedEmail.from?.name" label="From (name)" :value="parsedEmail.from?.name" /> |
| 89 | + <input-copyable v-if="parsedEmail.from" label="From (address)" :value="parsedEmail.from?.address || parsedEmail.from" /> |
| 90 | + <input-copyable v-if="parsedEmail.to" label="To" :value="JSON.stringify(parsedEmail.to)" /> |
| 91 | + <input-copyable v-if="parsedEmail.cc" label="Cc" :value="JSON.stringify(parsedEmail.cc)" /> |
| 92 | + <input-copyable v-if="parsedEmail.bcc" label="Bcc" :value="JSON.stringify(parsedEmail.bcc)" /> |
| 93 | + <input-copyable v-if="parsedEmail.replyTo" label="Reply-To" :value="JSON.stringify(parsedEmail.replyTo)" /> |
| 94 | + <input-copyable v-if="parsedEmail.subject" label="Subject" :value="parsedEmail.subject" /> |
| 95 | + <c-card v-if="parsedEmail.text" title="Plain Content" mb-2> |
| 96 | + <details> |
| 97 | + <summary>See content</summary> |
| 98 | + <textarea-copyable :value="parsedEmail.text" word-wrap /> |
| 99 | + </details> |
| 100 | + </c-card> |
| 101 | + <c-card v-if="parsedEmail.html" title="Html Content" mb-2> |
| 102 | + <details> |
| 103 | + <summary>See content</summary> |
| 104 | + <textarea-copyable :value="parsedEmail.html" word-wrap /> |
| 105 | + </details> |
| 106 | + </c-card> |
| 107 | + <c-card v-if="parsedEmail?.attachments?.length" title="Attachments" mb-2> |
| 108 | + <n-table> |
| 109 | + <thead> |
| 110 | + <tr> |
| 111 | + <th scope="col"> |
| 112 | + Attachment |
| 113 | + </th><th scope="col" /> |
| 114 | + </tr> |
| 115 | + </thead> |
| 116 | + <tbody> |
| 117 | + <tr |
| 118 | + v-for="(h, index) in parsedEmail.attachments || []" |
| 119 | + :key="index" |
| 120 | + > |
| 121 | + <td> |
| 122 | + {{ `${h.filename || h.contentId || 'noname'} (${h.mimeType}) / ${h.disposition}` }} |
| 123 | + </td> |
| 124 | + <td> |
| 125 | + <c-button @click="downloadFile(h.content, h.filename || h.contentId || 'noname', h.mimeType)"> |
| 126 | + Download |
| 127 | + </c-button> |
| 128 | + </td> |
| 129 | + </tr> |
| 130 | + </tbody> |
| 131 | + </n-table> |
| 132 | + </c-card> |
| 133 | + |
| 134 | + <input-copyable v-if="parsedEmail.messageId" label="Message Id" :value="parsedEmail.messageId" /> |
| 135 | + <input-copyable v-if="parsedEmail.inReplyTo" label="In Reply To" :value="parsedEmail.inReplyTo" /> |
| 136 | + <input-copyable v-if="parsedEmail.references" label="References" :value="parsedEmail.references" /> |
| 137 | + <input-copyable v-if="parsedEmail.deliveredTo" label="Delivered To" :value="parsedEmail.deliveredTo" /> |
| 138 | + <input-copyable v-if="parsedEmail.returnPath" label="Return Path" :value="parsedEmail.returnPath" /> |
| 139 | + <input-copyable v-if="parsedEmail.sender?.name" label="Sender (name)" :value="parsedEmail.sender?.name" /> |
| 140 | + <input-copyable v-if="parsedEmail.sender" label="Sender (address)" :value="parsedEmail.sender?.address || parsedEmail.sender" /> |
| 141 | + |
| 142 | + <c-card title="All Headers" mt-2> |
| 143 | + <input-copyable |
| 144 | + v-for="(h, index) in parsedEmail.headers || []" |
| 145 | + :key="index" |
| 146 | + :label="h.key" |
| 147 | + :value="h.value" |
| 148 | + /> |
| 149 | + </c-card> |
| 150 | + </c-card> |
| 151 | + </div> |
| 152 | +</template> |
0 commit comments