2017-05-30 16 views
4

我試圖從Firebase雲端函數使用Mailgun的api發送電子郵件。我曾嘗試在Cloud Function中實現同樣的nodejs教程,但我總是得到「錯誤:無法處理請求」。請問我做錯了什麼。從角色2應用程序的Firebase雲端函數發送mailgun電子郵件

雲功能如下代碼:

<pre> 
    <code> 
var functions = require('firebase-functions'); 

var nodemailer = require('nodemailer'); 

    var auth = { 
    auth: { 
     api_key: '###################', 
     domain: 's###############g' 
    } 
} 
exports.helloWorld = functions.https.onRequest((request, response) => { 
    response.send("Hello from Firebase!"); 
    }); 

    var nodemailerMailgun = nodemailer.createTransport(auth); 

exports.sendEmail = functions.https.onRequest((request, response) =>{ 
    //app.get('/', function(req, res) { 
    test(); 
}); 

    function test(){ 
    const mailOptions = { 
     //Specify email data 
      from: "[email protected]", 
      //The email to contact 
     to: "[email protected]", 
     //Subject and text data 
     subject: 'Hello from Mailgun', 
     text: 'Hello, This is not a plain-text email, I wanted to test  some spicy Mailgun sauce in NodeJS! <a href="http://0.0.0.0:3030/validate?' +  req.params.mail + '">Click here to add your email address to a mailing  list</a>' 
    }; 
    return smtpTransport.sendMail(mailOptions).then(() => { 
    console.log("It works"); 
    }); 
} 
</pre> 

感謝您的幫助。

+0

感謝編輯@AL –

+0

你在火力地堡的付費訂閱的一個? Firebase只允許在付費計劃中使用非Google出站網絡請求。 –

+0

@GokulKathirvel。還沒。這可能是問題嗎? –

回答

1

正如@GokulKathirvel所述,只有付費帳戶纔會發送出站電子郵件。但是我能夠證明功能儀表板中的功能。您會收到以下消息的功能被觸發時:

Billing account not configured. External network is not accessible and quotas are severely limited. Configure billing account to remove these restrictions

有了這樣的方式,你也應該能夠在使用節點包mailgun-js做。

var functions = require('firebase-functions') 
var mailgun = require('mailgun-js')({apiKey, domain}) 

exports.sendWelcomeEmail = functions.database.ref('users/{uid}').onWrite(event => { 

    // only trigger for new users [event.data.previous.exists()] 
    // do not trigger on delete [!event.data.exists()] 
    if (!event.data.exists() || event.data.previous.exists()) { 
    return 
    } 

    var user = event.data.val() 
    var {email} = user 

    var data = { 
    from: '[email protected]', 
    subject: 'Welcome!', 
    html: `<p>Welcome! ${user.name}</p>`, 
    'h:Reply-To': '[email protected]', 
    to: email 
    } 

    mailgun.messages().send(data, function (error, body) { 
    console.log(body) 
    }) 
}) 

來源https://www.automationfuel.com/firebase-functions-sending-emails/

+1

非常感謝@Marcos –

相關問題