Restructure progress
This commit is contained in:
88
src/lib/google/gmail/server.ts
Normal file
88
src/lib/google/gmail/server.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
import { google } from 'googleapis';
|
||||
import quotedPrintable from 'quoted-printable';
|
||||
import { getAuthenticatedClient } from '../auth/server.js';
|
||||
|
||||
/**
|
||||
* Create an HTML email template
|
||||
* @param text - Email body text
|
||||
* @returns HTML email template
|
||||
*/
|
||||
export function createEmailTemplate(text: string): string {
|
||||
return `<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<style>
|
||||
@import url('https://fonts.googleapis.com/css2?family=Lato&display=swap');
|
||||
</style>
|
||||
</head>
|
||||
<body style="font-family: 'Lato', sans-serif; background-color: #f9f9f9; padding: 20px; margin: 0;">
|
||||
<div style="max-width: 600px; margin: auto; background: white; padding: 20px; border-radius: 8px;">
|
||||
<p style="white-space: pre-line; font-size: 16px; color: #333;">${text}</p>
|
||||
<img src="cid:qrCode1" alt="QR Code" style="display: block; margin: 20px auto; max-width: 50%; height: auto;" />
|
||||
</div>
|
||||
</body>
|
||||
</html>`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send an email through Gmail
|
||||
* @param refreshToken - Google refresh token
|
||||
* @param params - Email parameters (to, subject, text, qr_code)
|
||||
*/
|
||||
export async function sendGmail(
|
||||
refreshToken: string,
|
||||
{
|
||||
to,
|
||||
subject,
|
||||
text,
|
||||
qr_code
|
||||
}: {
|
||||
to: string;
|
||||
subject: string;
|
||||
text: string;
|
||||
qr_code: string;
|
||||
}
|
||||
) {
|
||||
const oauth = getAuthenticatedClient(refreshToken);
|
||||
const gmail = google.gmail({ version: 'v1', auth: oauth });
|
||||
|
||||
const message_html = createEmailTemplate(text);
|
||||
const boundary = 'BOUNDARY';
|
||||
const nl = '\r\n';
|
||||
|
||||
const htmlBuffer = Buffer.from(message_html, 'utf8');
|
||||
const htmlLatin1 = htmlBuffer.toString('latin1');
|
||||
const htmlQP = quotedPrintable.encode(htmlLatin1);
|
||||
const qrLines = qr_code.replace(/.{1,76}/g, '$&' + nl);
|
||||
|
||||
const rawParts = [
|
||||
'MIME-Version: 1.0',
|
||||
`To: ${to}`,
|
||||
`Subject: ${subject}`,
|
||||
`Content-Type: multipart/related; boundary="${boundary}"`,
|
||||
'--' + boundary,
|
||||
'Content-Type: text/html; charset="UTF-8"',
|
||||
'Content-Transfer-Encoding: quoted-printable',
|
||||
'',
|
||||
htmlQP,
|
||||
'',
|
||||
'--' + boundary,
|
||||
'Content-Type: image/png',
|
||||
'Content-Transfer-Encoding: base64',
|
||||
'Content-ID: <qrCode1>',
|
||||
'Content-Disposition: inline; filename="qr.png"',
|
||||
'',
|
||||
qrLines,
|
||||
'',
|
||||
'--' + boundary + '--',
|
||||
''
|
||||
];
|
||||
|
||||
const rawMessage = rawParts.join(nl);
|
||||
const raw = Buffer.from(rawMessage).toString('base64url');
|
||||
|
||||
await gmail.users.messages.send({
|
||||
userId: 'me',
|
||||
requestBody: { raw }
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user