function createMailConnections({ ImapFlow, createTransport, getImapFlow, getNodemailer, oauthTokenManager }) { let ImapFlowModule = ImapFlow; let nodemailerModule = createTransport ? { createTransport } : null; async function imapClient(account, options = {}) { if (!ImapFlowModule) { const module = await getImapFlow(); ImapFlowModule = module.ImapFlow || module.default || module; } const isOAuth = account.authMethod === 'oauth2'; const auth = isOAuth ? { user: account.email, accessToken: await oauthTokenManager.getAccessToken(account) } : { user: account.email, pass: account.password }; return new ImapFlowModule({ host: account.imapHost, port: account.imapPort, secure: Boolean(account.imapSecure), doSTARTTLS: !account.imapSecure, auth, tls: { rejectUnauthorized: true }, logger: false, connectionTimeout: 15000, greetingTimeout: 10000, socketTimeout: 20000, ...options }); } async function smtpTransport(account) { if (!nodemailerModule) { nodemailerModule = await getNodemailer(); } const isOAuth = account.authMethod === 'oauth2'; const auth = isOAuth ? { type: 'OAuth2', user: account.email, accessToken: await oauthTokenManager.getAccessToken(account), expires: account.oauthAccessTokenExpiresAt } : { user: account.email, pass: account.password }; const secure = Boolean(account.smtpSecure); return nodemailerModule.createTransport({ host: account.smtpHost, port: account.smtpPort, secure, requireTLS: !secure, auth, tls: { rejectUnauthorized: true, servername: account.smtpHost }, connectionTimeout: 15000, greetingTimeout: 10000, socketTimeout: 20000 }); } return Object.freeze({ imapClient, smtpTransport }); } module.exports = { createMailConnections };