在一些帮助下,我成功创建了一个云函数,该函数发送电子邮件onCreate的另一个文档。我的意图是这封电子邮件显示了文档中的数据。我的方法是在SendGrid模板中使用替换,并将这些替换绑定到Firestore中的文档中的数据。
Firestore将其数据存储在/ requests下,每个doc都有一个随机创建的ID。每次提交表单时都会创建一个doc。
至于现在,我成功收到了一封电子邮件,但没有想要的数据/替换。
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const SENDGRID_API_KEY = functions.config().sendgrid.key;
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(SENDGRID_API_KEY);
exports.firestoreRequest = functions.firestore.document('requests/{requestId}')
.onCreate((snap, context) => {
const requestId = snap.id; // get the id
const db = admin.firestore();
return db.collection('requests').doc(requestId)
.get()
.then(doc => {
const request = doc.data();
const msg = {
to: '[email protected]',
from: '[email protected]',
templateId: 'd-3cd6b40a74f34b53d1633702107d2',
substitutionWrappers: ['{{', '}}'],
substitutions: {
name: request.name,
lastname: request.lastname,
email: request.email,
package: request.package,
date: request.date,
text: request.text
// and other custom properties here
}
};
return sgMail.send(msg)
})
.then(() => console.log('email sent!') )
.catch(err => console.log(err) )
});
SendGrid模板:
<html>
<head>
<title>Request</title>
</head>
<body>
Name: {{name}}<br>
Family: {{lastname}}<br>
Email: {{email}}<br>
Package: {{package}}<br>
Date: {{date}}<br>
Text: {{text}}<br>
</body>
</html>
我已经发现我指的是错误的SendGrid模板类型。如果我错了,请纠正我,但SendGrid不再支持旧版电子邮件模板,这正是我所指的替换模板的类型。
此设置是指我们的原始电子邮件模板。我们现在支持更多功能齐全的事务模板,支持多个模板,版本控制等。
而不是我必须使用的替换:
dynamic_template_data: {
name: request.name,
lastname: request.lastname,
email: request.email,
package: request.package,
date: request.date,
text: request.text
// and other custom properties here
}