I am trying to send emails from a firebase
function that I call from my vue
app. My firebase function looks like:
JavaScript
x
45
45
1
const functions = require('firebase-functions');
2
const admin = require("firebase-admin")
3
const nodemailer = require('nodemailer');
4
5
admin.initializeApp()
6
7
8
//google account credentials used to send email
9
var transporter = nodemailer.createTransport({
10
host: 'smtp.gmail.com',
11
port: 465,
12
secure: true,
13
auth: {
14
user: 'xxxxx@gmail.com',
15
pass: 'xxxxxxxxxx'
16
}
17
});
18
19
20
exports.sendMail = functions.https.onRequest((req, res) => {
21
res.header("Access-Control-Allow-Origin", "*");
22
res.header("Access-Control-Allow-Headers", "Content-Type");
23
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
24
const mailOptions = {
25
from: `•••••••••@gmail.com`,
26
to: req.body.email,
27
subject: 'contact form message',
28
html: `<h1>Order Confirmation</h1>
29
<p>
30
<b>Email: </b>"hello<br>
31
</p>`
32
};
33
34
35
return transporter.sendMail(mailOptions, (error, data) => {
36
console.log("here")
37
if (error) {
38
return res.send({"data": error.toString()});
39
}
40
data = JSON.stringify(data)
41
return res.send({"data": `Sent! ${data}`});
42
});
43
44
});
45
I removed the user and pass values here. They have the correct value in my code. The vue
method I use to call this function looks like:
JavaScript
1
13
13
1
sendEmail(){
2
let sendMail = firebase.functions().httpsCallable('sendMail');
3
sendMail(
4
{
5
email: 'xx@xx.xx.xx'
6
}
7
).then(
8
result => {
9
console.log(result.data)
10
}
11
)
12
}
13
When this function is called I get the following error in the console:
JavaScript
1
7
1
Uncaught (in promise) Error: Response is not valid JSON object.
2
at new e (index.cjs.js:58)
3
at t.<anonymous> (index.cjs.js:543)
4
at u (tslib.es6.js:100)
5
at Object.next (tslib.es6.js:81)
6
at s (tslib.es6.js:71)
7
Any help would be appreciated. Thanks
Advertisement
Answer
You must a return a valid JSON
result like this:
return res.status(200).json({});
Here the JSON is
{}
but it could be for instance something like {"status": "success}
.