2017-10-04 87 views
1

我試圖使用nodemailer通過A G套房的電子郵件地址從服務器發送電子郵件

我現在有一個域名與谷歌域以及與此註冊我有A G套房使Node.js服務器發送電子郵件實例設置爲我提供了一個電子郵件服務器。

我有一個電子郵件設置:[email protected]

什麼即時試圖做的是從電子郵件從我的服務器發送電子郵件以上。我不想使用普通用戶和密碼驗證,因爲這對我來說是非常不安全的。

有沒有人有一個教程或文件,他們可以鏈接我,這將幫助我實現這一目標?

回答

1

下面是來自官方Gmail API documentation一個Quickstart

完成本頁面的其餘部分中描述的步驟,並在大約 五分鐘就會有一個簡單的Node.js的命令行應用程序 ,使請求到Gmail的API。從快速入門

段:

var fs = require('fs'); 
var readline = require('readline'); 
var google = require('googleapis'); 
var googleAuth = require('google-auth-library'); 

// If modifying these scopes, delete your previously saved credentials 
// at ~/.credentials/gmail-nodejs-quickstart.json 
var SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']; 
var TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH || 
    process.env.USERPROFILE) + '/.credentials/'; 
var TOKEN_PATH = TOKEN_DIR + 'gmail-nodejs-quickstart.json'; 

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

/** 
* 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) { 
    var clientSecret = credentials.installed.client_secret; 
    var clientId = credentials.installed.client_id; 
    var redirectUrl = credentials.installed.redirect_uris[0]; 
    var auth = new googleAuth(); 
    var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl); 

    // Check if we have previously stored a token. 
    fs.readFile(TOKEN_PATH, function(err, token) { 
    if (err) { 
     getNewToken(oauth2Client, callback); 
    } else { 
     oauth2Client.credentials = JSON.parse(token); 
     callback(oauth2Client); 
    } 
    }); 
} 

/** 
* Get and store new token after prompting for user authorization, and then 
* execute the given callback with the authorized OAuth2 client. 
* 
* @param {google.auth.OAuth2} oauth2Client The OAuth2 client to get token for. 
* @param {getEventsCallback} callback The callback to call with the authorized 
*  client. 
*/ 
function getNewToken(oauth2Client, callback) { 
    var authUrl = oauth2Client.generateAuthUrl({ 
    access_type: 'offline', 
    scope: SCOPES 
    }); 
    console.log('Authorize this app by visiting this url: ', authUrl); 
    var rl = readline.createInterface({ 
    input: process.stdin, 
    output: process.stdout 
    }); 
    rl.question('Enter the code from that page here: ', function(code) { 
    rl.close(); 
    oauth2Client.getToken(code, function(err, token) { 
     if (err) { 
     console.log('Error while trying to retrieve access token', err); 
     return; 
     } 
     oauth2Client.credentials = token; 
     storeToken(token); 
     callback(oauth2Client); 
    }); 
    }); 
} 

/** 
* Store token to disk be used in later program executions. 
* 
* @param {Object} token The token to store to disk. 
*/ 
function storeToken(token) { 
    try { 
    fs.mkdirSync(TOKEN_DIR); 
    } catch (err) { 
    if (err.code != 'EEXIST') { 
     throw err; 
    } 
    } 
    fs.writeFile(TOKEN_PATH, JSON.stringify(token)); 
    console.log('Token stored to ' + TOKEN_PATH); 
} 

/** 
* Lists the labels in the user's account. 
* 
* @param {google.auth.OAuth2} auth An authorized OAuth2 client. 
*/ 
function listLabels(auth) { 
    var gmail = google.gmail('v1'); 
    gmail.users.labels.list({ 
    auth: auth, 
    userId: 'me', 
    }, function(err, response) { 
    if (err) { 
     console.log('The API returned an error: ' + err); 
     return; 
    } 
    var labels = response.labels; 
    if (labels.length == 0) { 
     console.log('No labels found.'); 
    } else { 
     console.log('Labels:'); 
     for (var i = 0; i < labels.length; i++) { 
     var label = labels[i]; 
     console.log('- %s', label.name); 
     } 
    } 
    }); 
} 

要理解這個概念和實現細節,你可以嘗試Sending Email。爲了給您一個概述,這裏是發送電子郵件和高端工作流程的方式。

有兩種方式使用Gmail的API發送電子郵件:

電子郵件作爲base64url編碼字符串發送到message resource的原始屬性 中。高級別工作流程發送電子郵件 是:

  1. 創建一些方便的方式電子郵件內容,並對其進行編碼,作爲base64url字符串。
  2. 創建一個新的信息資源及其raw屬性設置爲您剛剛創建的base64url字符串。
  3. 呼叫messages.send,或者,如果發送草案,drafts.send發送消息。

您也可以參考本教程Nodemailer community site

這些文檔適用於舊版本的Nodemailer v2和 的未維護版本。對於升級和最新Nodemailer V3 +文檔, 看到nodemailer.com主頁。

0

而是建立整個邏輯創建令牌,你可以簡單地使用Passportjs google auth,最後用googleapis包發送電子郵件。

相關問題