Search code examples
javascriptfirebasegoogle-cloud-functionsnodemailer

Nodemailer google cloud function error code: 304


I have a firebase google cloud function that sends an email via javascript with cors and nodemailer.

I am getting an error code: 304 in some occasions with different destination emails. Why would this be happening occasionally and only when I change the email destination. Sometimes its works and sometimes it doesnt.

How can the condition be sometimes false by changing the email send to destination? Do i need to set the cache somehow in the function?

  • Allow Un-secure apps is enabled followed by Captcha Unlocked

error code: 304

notModified - The conditional request would have been successful, but the condition was false, so no body was sent.

enter image description here

const nodemailer = require('nodemailer');
const cors = require('cors')({origin: true});


// Gmail configuration to Send eMail
let transporter = nodemailer.createTransport({
    host: 'smtp.gmail.com',
    port: 587,
    secure: false,
    auth: {
        user: 'email',
        pass: 'passwors'
    }
});



exports.sendMailPasswordReset = functions.https.onRequest((req, res) => {

    cors(req, res, () => {


    const mailOptions = {
        from: 'team <team@example.app>', // Something like: Jane Doe <janedoe@gmail.com>
        to: 'example@gmail.com',
        subject: 'Password Reset', // email subject
        html: `html body` // email content in HTML
    };

    // returning result
    return transporter.sendMail(mailOptions, (erro, info) => {
        if(erro){
            return res.send(erro.toString());
        }
        return res.send('true');
});
});
});

Solution

  • You cannot send back a constant value.

    return res.send('true');
    

    Error code 304: Is returning false because it is always returning the same constant.

    To fix the problem the send value needs to be different every time.

    For Example:

    let random = Math.random().toString(36).substring(7);
    return res.send('true_' + random);
    

    I also added for cors

    res.setHeader('Cache-Control', 'no-cache');
    

    Here is a detailed understanding of Error code 304