-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: integrate mailer service directly in mailpress
- Loading branch information
Showing
11 changed files
with
13,965 additions
and
250 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { MailOptions, OAuthOptions } from "./types"; | ||
|
||
export async function sendMail( | ||
mailOptions: MailOptions, | ||
oauthOptions: OAuthOptions | ||
): Promise<object> { | ||
const apiUrl = "https://graph.microsoft.com/v1.0/me/sendMail"; | ||
|
||
const headers = { | ||
Authorization: `Bearer ${oauthOptions.accessToken}`, | ||
"Content-Type": "application/json", | ||
}; | ||
|
||
const requestBody = JSON.stringify({ | ||
message: { | ||
subject: mailOptions.subject, | ||
from: { emailAddress: { address: mailOptions.from } }, | ||
body: { | ||
contentType: mailOptions.html ? "html" : "text", | ||
content: mailOptions.html || mailOptions.text, | ||
}, | ||
toRecipients: mailOptions.to.map((to) => ({ | ||
emailAddress: { address: to }, | ||
})), | ||
...(mailOptions.replyTo && { | ||
replyTo: [{ emailAddress: { address: mailOptions.replyTo } }], | ||
}), | ||
}, | ||
saveToSentItems: true, | ||
}); | ||
|
||
console.log(requestBody); | ||
|
||
const response = await fetch(apiUrl, { | ||
method: "POST", | ||
headers: headers, | ||
body: requestBody, | ||
}); | ||
|
||
if (!response.ok) { | ||
const error = await response.json(); | ||
throw new Error(`Failed to send email: ${error.error.message}`); | ||
} | ||
|
||
return response; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { MailOptions, OAuthOptions } from "./types"; | ||
|
||
export async function sendMail( | ||
mailOptions: MailOptions, | ||
oauthOptions: OAuthOptions | ||
): Promise<object> { | ||
const apiUrl = "https://gmail.googleapis.com/gmail/v1/users/me/messages/send"; | ||
|
||
const headers = { | ||
Authorization: `Bearer ${oauthOptions.accessToken}`, | ||
"Content-Type": "application/json", | ||
}; | ||
|
||
const message = | ||
`From: ${mailOptions.from}\n` + | ||
`To: ${mailOptions.to.join(",")}\n` + | ||
`Subject: ${mailOptions.subject}\n\n` + | ||
`${mailOptions.html || mailOptions.text}`; | ||
|
||
const requestBody = JSON.stringify({ | ||
raw: Buffer.from(message) | ||
.toString("base64") | ||
.replace(/\+/g, "-") | ||
.replace(/\//g, "_"), | ||
}); | ||
|
||
const response = await fetch(apiUrl, { | ||
method: "POST", | ||
headers: headers, | ||
body: requestBody, | ||
}); | ||
|
||
if (!response.ok) { | ||
const error = await response.json(); | ||
throw new Error(`Failed to send email: ${error.error.message}`); | ||
} | ||
|
||
return response; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import nodemailer from "nodemailer"; | ||
import inlineBase64 from "nodemailer-plugin-inline-base64"; | ||
import { SentMessageInfo } from "nodemailer/lib/smtp-connection"; | ||
import { MailOptions } from "./types"; | ||
|
||
export interface SMTPOptions { | ||
host: string; | ||
port: number; | ||
secure: boolean; | ||
user: string; | ||
password: string; | ||
} | ||
|
||
async function createConnection({ | ||
host, | ||
port, | ||
secure, | ||
user, | ||
password, | ||
}: SMTPOptions) { | ||
return new Promise<nodemailer.Transporter>((resolve, reject) => { | ||
resolve( | ||
nodemailer.createTransport({ | ||
host: host, | ||
port, | ||
secure: secure, | ||
auth: { | ||
user: user, | ||
pass: password, | ||
}, | ||
}) | ||
); | ||
}); | ||
} | ||
|
||
// SEND MAIL | ||
export async function sendMail( | ||
mailOptions: MailOptions, | ||
smtpOptions: SMTPOptions | ||
): Promise<SentMessageInfo> { | ||
const connection = await createConnection(smtpOptions); | ||
connection.use("compile", inlineBase64({ cidPrefix: "snek_" })); | ||
|
||
console.log(mailOptions); | ||
|
||
const mail = await connection.sendMail(mailOptions); | ||
|
||
console.log(mail); | ||
|
||
return mail; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export interface MailOptions { | ||
from: string; | ||
to: string[]; | ||
replyTo?: string; | ||
subject: string; | ||
text: string; | ||
html?: string; | ||
} | ||
|
||
export interface OAuthOptions { | ||
accessToken: string; | ||
} |