Agustin Barros
Agustin Barros

Reputation: 95

Email Trigger from Firebase Message and Subject empty

enter image description hereI installed and I'm testing the Email Trigger extension from Firebase. This extension created a "mail" collection on Firestore, where I add a new document using the fields "to", "Message" and "Subject". I can receive the email but the problems are that the Subject and message comes empty. I'm testing the extension just filling the fields from the Firestore database, not from the code. Does anybody know the right format structure to fill those fills? (subject and message). Please see the image attached. Thanks.

Upvotes: 0

Views: 546

Answers (3)

bergin
bergin

Reputation: 1604

What you need to do is set the to field to your email and the message should be a map type. Then add fields subject & text both as string and you should find it works.

Upvotes: 0

Hugo Barbosa
Hugo Barbosa

Reputation: 153

This is the complete and updated example making use of Firebase SDK 9.x.x

import { initializeApp } from "https://www.gstatic.com/firebasejs/9.1.0/firebase-app.js";
import { getAuth, onAuthStateChanged } from 'https://www.gstatic.com/firebasejs/9.1.0/firebase-auth.js';
import { getFirestore, addDoc, collection } from 'https://www.gstatic.com/firebasejs/9.1.0/firebase-firestore-lite.js';

// Your web app's Firebase configuration
const firebaseConfig = {
...
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);
const auth = getAuth();
const db = getFirestore(app);

onAuthStateChanged(auth, (user) => {
if (user) {
const uid = user.uid;
console.log('User is logged in.');
} else {
// User is signed out
console.log('User is NOT logged in.');
}
});

try {
const docRef = await addDoc(collection(db, "mail"), {
to: "[email protected]",
message: {
subject: 'Hello from Firebase!',
html: 'This is an <code>HTML</code> email body.',
}
});
console.log("Document written with ID: ", docRef.id);
} catch (e) {
console.error("Error adding document: ", e);
}

Upvotes: 0

Michael Bleigh
Michael Bleigh

Reputation: 26343

Your document is not the correct format. The message field should be an object containing subject, html and/or text bodies:

admin.firestore().collection('mail').add({
  to: '[email protected]',
  message: {
    subject: 'Hello from Firebase!',
    html: 'This is an <code>HTML</code> email body.',
  },
})

You are instead supplying message as a string. Read the usage instructions for your installed extension for additional details.

Upvotes: 1

Related Questions