我无法远程发送电子邮件。我该如何解决?

时间:2020-07-21 11:06:55

标签: node.js heroku gmail nodemailer

我无法远程发送电子邮件。我不知道发生了什么。

function getNewToken(oAuth2Client, callback) {
    const authUrl = oAuth2Client.generateAuthUrl({
        access_type: 'offline',
        scope: SCOPES,
    });
    console.log('Authorize this app by visiting this url:', authUrl);
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout,
    });
    rl.question('Enter the code from that page here: ', (code) => {
        rl.close();
        oAuth2Client.getToken(code, (err, token) => {
            if (err) return console.error('Error retrieving access token', err);
            oAuth2Client.setCredentials(token);
            // Store the token to disk for later program executions
            fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
                if (err) return console.error(err);
                console.log('Token stored to', TOKEN_PATH);
            });
            callback(oAuth2Client);
        });
    });
}

// If modifying these scopes, delete token.json.
const SCOPES = ['https://www.googleapis.com/auth/gmail.readonly'];
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
const TOKEN_PATH = 'token.json';

// Load client secrets from a local file.
function customMethod() {
    fs.readFile('credentials.json', (err, content) => {
        if (err) return console.log('Error loading client secret file:', err);
        // Authorize a client with credentials, then call the Gmail API.
        authorize(JSON.parse(content), callbackAuthorize);
    });
}

/**
 * Create an OAuth2 client with the given credentials, and then execute the
 * given callback function.
 * @param {Object} credentials The authorization client credentials.
 * @param {function} callback The callback to call with the authorized client.
 */
function authorize(credentials, callback) {
    const { client_secret, client_id, redirect_uris } = credentials.installed;
    const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uris[0]);
    // Check if we have previously stored a token.
    fs.readFile(TOKEN_PATH, async (err, token) => {
        if (err) return getNewToken(oAuth2Client, callback);
        oAuth2Client.setCredentials(JSON.parse(token));
        callback(oAuth2Client);
    });
};

function callbackAuthorize(newCredentials) {
    transporter = nodemailer.createTransport(smtpTransport({
        service: "Gmail",
        logger: true,
        auth: {
            type: 'OAuth2',
            user: process.env.email,
            pass: process.env.passMail,
            clientId: process.env.CLIENT_ID,
            clientSecret: process.env.CLIENT_SECRET,
            accessToken: newCredentials.credentials.access_token,
            refreshToken: newCredentials.credentials.refresh_token
        },
    }));
    transporter.verify(function (error, success) {
        if(error) console.log(error);
    })
};

let transporter = nodemailer.createTransport({
    service: 'Gmail'
})

transporter.verify(function (error, success) {
    fs.readFile('credentials.json', (err, content) => {
        if (err) return console.log('Error loading client secret file:', err);
        // Authorize a client with credentials, then call the Gmail API.
        authorize(JSON.parse(content), callbackAuthorize);
    });
});

我正在使用Nodemailer和Gmail API生成新令牌,但是在Heroku上不起作用。当我在本地发送电子邮件时:

[2020-07-21 10:41:37] INFO: [uSaUpsnSpFg] Secure connection established to 64.233.190.109:465
database connected
[2020-07-21 10:41:37] DEBUG: [uSaUpsnSpFg] SMTP handshake finished
[2020-07-21 10:41:38] INFO: [uSaUpsnSpFg] User "clicouentregou@gmail.com" authenticated
[2020-07-21 10:41:38] DEBUG: [uSaUpsnSpFg] Closing connection to the server using "end"
[2020-07-21 10:52:02] INFO: [eeglknmtUY4] Secure connection established to 64.233.190.108:465
[2020-07-21 10:52:03] DEBUG: [eeglknmtUY4] SMTP handshake finished
[2020-07-21 10:52:03] INFO: [eeglknmtUY4] User "clicouentregou@gmail.com" authenticated
[2020-07-21 10:52:03] INFO: Sending message <3707772c-ca11-7c86-803a-c136962a611c@gmail.com> to <marchiartur@gmail.com>
[2020-07-21 10:52:04] INFO: [eeglknmtUY4] C: <2314 bytes encoded mime message (source size 2291 bytes)>
[2020-07-21 10:52:05] DEBUG: [eeglknmtUY4] Closing connection to the server using "end

当我在Heroku上发送电子邮件时:

Secure connection established to 172.253.63.108:465
SMTP handshake finished
User "clicouentregou@gmail.com" failed to authenticate
Closing connection to the server using "end"
Error: Invalid login: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbv

如果我访问以下链接:https://accounts.google.com/b/0/DisplayUnlockCaptcha,它会起作用一段时间,但是我想自动执行此操作。我该怎么办?

0 个答案:

没有答案