从Firebase Webapp发送邮件 [英] Sending mail from Firebase webapp

查看:130
本文介绍了从Firebase Webapp发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

< pre class = snippet-code-js lang-js prettyprint-override> var express = require('express'); var app = express(); var nodemailer = require('nodemailer'); var transporter = nodemailer.createTransport('smtps://username@gmail.com:password@smtp.gmail.com'); app.get('/ sendMail',function(req,res){var mailOptions = {to: 'receiver@gmail.com',主题:测试邮件,html:测试邮件} transporter.sendMail(mailOptions,function(err,response){if(err){res.end('未发送邮件' );} else {res.end('邮件已发送');}});})。listen(9091); console.log('This is running');



我正在开发Firebase Web应用程序。我需要通过我的Web应用发送电子邮件。我看了一些教程,找到了一些代码。使用上面的代码,我能够发送电子邮件。它在端口 9091 中运行。当地址栏具有 www.mywebsite.com/sendMail 时,将发送该消息。但是,当我将此页面托管到Firebase时,此代码需要进行哪些更改,端口号是否必须更改?请帮助在我的Web应用程序中实现此代码。

解决方案

更新2019:或者,您可以使用新的触发电子邮件Firebase扩展(当前处于测试版)。






我在评论中提到,问题是:您的Web应用程序是使用NodeJS构建的,并且您试图将其托管在静态托管环境( Firebase托管)中。但是Firebase还具有云函数-该服务可让您在NodeJS环境中运行函数。因此,您应该改为将应用程序部署到该服务。



云功能入门。而且,由于您已经设置了Firebase托管,因此只需使用以下命令即可转到该目录并设置Cloud Functions:

  firebase初始化函数

将创建一个名为 functions的新文件夹,并创建一个 index.js 文件将自动添加到该文件夹​​。现在这是您要编写函数的文件(实际上,我们只需要1个函数由HTTP触发发送电子邮件)。



因此,要创建此功能,您将不再需要Express。您可以将其删除,然后稍稍更改index.js文件以添加firebase-functions,这样它将变成:

  / / var express = require('express');不再需要
// var app = express();不再需要
var nodemailer = require('nodemailer');
const functions = require('firebase-functions');

var transporter = nodemailer.createTransport(’smtps://username@gmail.com:password@smtp.gmail.com’);

exports.sendMail =函数。https.onRequest((req,res)= >> {
var mailOptions = {
至:'receiver@gmail.com',
主题:测试邮件,
html:测试邮件
}
transporter.sendMail(mailOptions,function(err,response){
if(err ){
res.end('邮件未发送');
}
else {
res.end('邮件发送');
}
});
});

现在,当您要发送消息时,可以使用以下URL: https:// us-central1-< project-id> .cloudfunctions.net / sendMail



替换< ; project-id> 与您的Firebase项目ID。


var express = require('express');
var app =  express();
var nodemailer=require('nodemailer');

var transporter = nodemailer.createTransport('smtps://username@gmail.com:password@smtp.gmail.com');
app.get('/sendMail', function(req,res){
	var mailOptions={
		to: 'receiver@gmail.com',
		subject: 'Test Mail',
		html: 'Testing the Mail'
	}
	transporter.sendMail(mailOptions,function(err,response){
		if(err){
			res.end('Mail not sent');
		}
		else{
			res.end('Mail sent');
		}
	});
}).listen(9091);

console.log('This is running');

I am developing a firebase web app. I need to send an email via my web app. I saw some tutorials and found some code. Using the above code I am able to send emails. It runs in the port 9091. The message will be sent when the address bar has www.mywebsite.com/sendMail. But when I host this page to Firebase what changes to be done in this code, whether there must be a change in the port number?. Please help to implement this code in my web app.

解决方案

Update 2019: Alternatively, you can use the new Trigger Email Firebase Extension (currently in beta).


As I mentioned on my comment, the problem is: your web app was built with NodeJS and you're trying to host it on a static host environment (Firebase Hosting). But Firebase also has Cloud Functions - a service that let's you run functions on a NodeJS environment. So you should deploy your app to that service instead.

It's really easy to Get Started with Cloud Functions. And since you have already set up Firebase hosting, you can simply go to that directory and set up Cloud Functions by using this command:

firebase init functions

A new folder named "functions" will be created and an index.js file will be automatically added to that folder. Now that's the file where you're going to write your functions (we're actually just gonna need 1 function triggered by HTTP to send the email).

So in order to create this function, you would no longer need Express. you can remove that and then change a bit your index.js file to add firebase-functions, so it would become:

//var express = require('express'); No longer needed
//var app =  express(); No longer needed
var nodemailer=require('nodemailer');
const functions = require('firebase-functions');

var transporter = nodemailer.createTransport('smtps://username@gmail.com:password@smtp.gmail.com');

exports.sendMail = functions.https.onRequest((req, res) =>{
    var mailOptions={
        to: 'receiver@gmail.com',
        subject: 'Test Mail',
        html: 'Testing the Mail'
    }
    transporter.sendMail(mailOptions,function(err,response){
        if(err){
            res.end('Mail not sent');
        }
        else{
            res.end('Mail sent');
        }
    });
});

Now when you want to send a message, you can use this url: https://us-central1-<project-id>.cloudfunctions.net/sendMail

Replace <project-id> with your Firebase Project ID.

这篇关于从Firebase Webapp发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆