Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

show email attachments #36

Merged
merged 3 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,33 @@ app.get("/emails/:id", async (req, res, next) => {
try {
const messages = await fetchMessages();
const message = messages[req.params.id];

const email = await createEmail(message);
const attachments = email.attachments.filter((attachment) => attachment.contentDisposition === "attachment");

res.render("email", {
subject: email.subject,
to: email.to,
htmlContent: email.html,
attachments,
messageId: req.params.id,
});
} catch (err) {
next(err);
}
});

app.get("/emails/:id/attachments/:attachmentId", async (req, res, next) => {
try {
const messages = await fetchMessages();
const message = messages[req.params.id];
const email = await createEmail(message);
const attachment = email.attachments.find((attachment) => attachment.partId === req.params.attachmentId);

res.set({
"Content-Type": attachment.contentType,
"Content-Disposition": `inline; filename="${attachment.filename}"`,
});
res.send(attachment.content);
} catch (err) {
next(err);
}
Expand Down
50 changes: 37 additions & 13 deletions test/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,43 @@ import app from "../app.js";

const generateMockEml = () => {
const timestamp = new Date().toUTCString();
return `Date: ${timestamp}\r\nFrom: [email protected]\r\nTo: [email protected]\r\nSubject: Test Email\r\n\r\nThis is the body of the email.`;
return `Date: Fri, 27 Sep 2024 00:18:19 +0300
From: [email protected]
To: [email protected]
Subject: Test Email
Content-Type: multipart/related;
boundary=b510f43f668889c35c6ed92270c106a8dc55a157b9add4fffd76983ad5cc

--b510f43f668889c35c6ed92270c106a8dc55a157b9add4fffd76983ad5cc
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=UTF-8

email body

--b510f43f668889c35c6ed92270c106a8dc55a157b9add4fffd76983ad5cc
Content-Disposition: attachment; filename="file.pdf"
Content-ID: <file.pdf>
Content-Transfer-Encoding: quoted-printable
Content-Type: image/pdf; name="file.pdf"

attachment-content
--b510f43f668889c35c6ed92270c106a8dc55a157b9add4fffd76983ad5cc--
`;
};

describe("App Tests", () => {
beforeEach(() => {
nock(`${process.env.LOCALSTACK_HOST || 'http://localhost:4566'}`)
nock(`${process.env.LOCALSTACK_HOST || "http://localhost:4566"}`)
.get("/_aws/ses")
.reply(200, {
messages: [
{
Timestamp: Date.now(),
RawData: generateMockEml(),
RawData: generateMockEml()
},
{
Timestamp: Date.now(),
RawData: generateMockEml(),
RawData: generateMockEml()
},
{
Timestamp: Date.now(),
Expand All @@ -29,8 +50,8 @@ describe("App Tests", () => {
},
Body: {
text_part: null,
html_part: "<html>This is a test email with html</html>",
},
html_part: "<html>This is a test email with html</html>"
}
},
{
Timestamp: Date.now(),
Expand All @@ -40,10 +61,10 @@ describe("App Tests", () => {
},
Body: {
text_part: "This is a test email",
html_part: null,
},
},
],
html_part: null
}
}
]
});
});

Expand Down Expand Up @@ -89,13 +110,16 @@ describe("App Tests", () => {
test("GET /emails/:id/download should return status 200 and download the email as a file", async () => {
const response = await supertest(app).get("/emails/0/download");
expect(response.status).toBe(200);
expect(response.header["content-disposition"]).toBe(
'attachment; filename="Test Email.eml"',
);
expect(response.header["content-disposition"]).toBe('attachment; filename="Test Email.eml"');
});

test("GET /emails/:id/download should return status 400 when the email does not contain raw data", async () => {
const response = await supertest(app).get("/emails/2/download");
expect(response.status).toBe(400);
});

test("GET /emails/:id/attachments/:attachmentId should return status 200 and show attachment", async () => {
const response = await supertest(app).get("/emails/1/attachments/2");
expect(response.status).toBe(200);
});
});
18 changes: 14 additions & 4 deletions views/email.pug
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,24 @@ html
padding: 10px 16px;
font-weight: normal;
}

.subject-value {
font-weight: bold;
}

.attachment-link {
color: #fff;
font-weight: bold;
}
body
.container
| Subject line:
="Subject line: "
span.subject-value #{subject} <br>
| To:
span.subject-value #{to}
="To: "
span.subject-value #{to} <br>
if attachments.length > 0
="Attachments: "
each att in attachments
a.attachment-link(href='/emails/' + messageId + '/attachments/' + att.partId, target="_blank")= att.filename
=" "
| !{htmlContent}