Skip to content

Commit b486b90

Browse files
committed
Allow single string to/cc/bcc fields
1 parent 30b4f21 commit b486b90

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

firestore-send-email/POSTINSTALL.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,20 @@ To test out this extension, add a document with a `to` field and a `message` fie
44

55
```
66
admin.firestore().collection('${param:MAIL_COLLECTION}').add({
7-
to: ['someone@example.com'],
7+
to: 'someone@example.com',
8+
message: {
9+
subject: 'Hello from Firebase!',
10+
text: 'This is the plaintext section of the email body.',
11+
html: 'This is the <code>HTML</code> section of the email body.',
12+
}
13+
}).then(() => console.log('Queued email for delivery!'));
14+
```
15+
16+
To send an email to multiple email addresses at once, provide an array to the `to` field:
17+
18+
```
19+
admin.firestore().collection('${param:MAIL_COLLECTION}').add({
20+
to: ['someone@example.com', 'someone-else@example.com'],
821
message: {
922
subject: 'Hello from Firebase!',
1023
text: 'This is the plaintext section of the email body.',
@@ -22,12 +35,12 @@ After its installation, this extension monitors all document writes to the `${pa
2235
The top-level fields of the document supply the email sender and recipient information. Available fields are:
2336

2437
* **from:** The sender's email address. If not specified in the document, uses the configured "Default FROM address" parameter.
25-
* **replyTo:** The reply-to email address. If not specified in the document, uses the configured "Default REPLY-TO address" parameter.
26-
* **to:** An array containing the recipient email addresses.
38+
* **replyTo:** The reply-to email address. If not specified in the document, uses the required configured "Default REPLY-TO address" parameter.
39+
* **to:** An email address or an array containing the recipient email addresses.
2740
* **toUids:** An array containing the recipient UIDs.
28-
* **cc:** An array containing the CC recipient email addresses.
41+
* **cc:** An email address or an array containing the CC recipient email addresses.
2942
* **ccUids:** An array containing the CC recipient UIDs.
30-
* **bcc:** An array containing the BCC recipient email addresses.
43+
* **bcc:** An email address or an array containing the BCC recipient email addresses.
3144
* **bccUids:** An array containing the BCC recipient UIDs.
3245

3346
**NOTE:** The `toUids`, `ccUids`, and `bccUids` options deliver emails based on user UIDs keyed to email addresses within a Cloud Firestore document. To use these recipient options, you need to specify a Cloud Firestore collection for the extension's "Users collection" parameter. The extension can then read the `email` field for each UID specified in the `toUids`, `ccUids`, and/or `bccUids` fields.

firestore-send-email/PREINSTALL.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Here's a basic example document write that would trigger this extension:
66

77
```js
88
admin.firestore().collection('mail').add({
9-
to: ['someone@example.com'],
9+
to: 'someone@example.com',
1010
message: {
1111
subject: 'Hello from Firebase!',
1212
html: 'This is an <code>HTML</code> email body.',

firestore-send-email/functions/src/index.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,19 +107,25 @@ async function preparePayload(payload: QueuePayload): Promise<QueuePayload> {
107107
let cc: string[] = [];
108108
let bcc: string[] = [];
109109

110-
if (payload.to) {
110+
if (typeof payload.to === 'string') {
111+
to = [payload.to];
112+
} else if (payload.to) {
111113
validateFieldArray("to", payload.to);
112-
to = [...to, ...payload.to];
114+
to = to.concat(payload.to);
113115
}
114116

115-
if (payload.cc) {
117+
if (typeof payload.cc === 'string') {
118+
cc = [payload.cc];
119+
} else if (payload.cc) {
116120
validateFieldArray("cc", payload.cc);
117-
cc = [...cc, ...payload.cc];
121+
cc = cc.concat(payload.cc);
118122
}
119123

120-
if (payload.bcc) {
124+
if (typeof payload.bcc === 'string') {
125+
bcc = [payload.bcc];
126+
} else if (payload.bcc) {
121127
validateFieldArray("bcc", payload.bcc);
122-
bcc = [...bcc, ...payload.bcc];
128+
bcc = bcc.concat(payload.bcc);
123129
}
124130

125131
if (!payload.toUids && !payload.ccUids && !payload.bccUids) {

0 commit comments

Comments
 (0)