diff --git a/packages/isomorphic-core/src/models/account.js b/packages/isomorphic-core/src/models/account.js index da19c9ae1..1af57971d 100644 --- a/packages/isomorphic-core/src/models/account.js +++ b/packages/isomorphic-core/src/models/account.js @@ -79,17 +79,34 @@ module.exports = (sequelize, Sequelize) => { }, smtpConfig() { - if (this.provider !== "imap") { - throw new Error("Non IMAP not yet supported") - } - - const {smtp_username, smtp_password} = this.decryptedCredentials(); const {smtp_host, smtp_port, ssl_required} = this.connectionSettings; - - return { - port: smtp_port, host: smtp_host, secure: ssl_required, - auth: { user: smtp_username, pass: smtp_password}, + const config = { + host: smtp_host, + port: smtp_port, + secure: ssl_required, } + + if (this.provider === "imap") { + const {smtp_username, smtp_password} = this.decryptedCredentials(); + config.auth = { user: smtp_username, pass: smtp_password} + } else if (this.provider === 'gmail') { + const {xoauth2} = this.decryptedCredentials(); + const {imap_username} = this.connectionSettings; + + // We have to unpack the access token from the entire XOAuth2 + // token because it is re-packed during the SMTP connection login. + // https://github.com/nodemailer/smtp-connection/blob/master/lib/smtp-connection.js#L1418 + const bearer = "Bearer "; + const decoded = atob(xoauth2); + const tokenIndex = decoded.indexOf(bearer) + bearer.length; + const token = decoded.substring(tokenIndex, decoded.length - 2); + + config.auth = { user: imap_username, xoauth2: token } + } else { + throw new Error(`${this.provider} not yet supported`) + } + + return config; }, supportsLabels() { diff --git a/packages/local-sync/src/local-api/routes/auth.js b/packages/local-sync/src/local-api/routes/auth.js index 76c95e1ba..609b97118 100644 --- a/packages/local-sync/src/local-api/routes/auth.js +++ b/packages/local-sync/src/local-api/routes/auth.js @@ -119,6 +119,9 @@ module.exports = (server) => { imap_username: email, imap_host: 'imap.gmail.com', imap_port: 993, + smtp_username: email, + smtp_host: 'smtp.gmail.com', + smtp_port: 465, ssl_required: true, } connectionCredentials = {