Skip to content

Commit 314efdf

Browse files
cabljacGustolandia
andcommitted
fix: add support for SendGrid categories and streamline SendGrid detection (#2053) (#2249)
Co-authored-by: Gustolandia <[email protected]>
1 parent 94fcf11 commit 314efdf

File tree

13 files changed

+3047
-3874
lines changed

13 files changed

+3047
-3874
lines changed

firestore-bigquery-export/functions/package-lock.json

+2,173-2,557
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

firestore-send-email/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## Version 0.1.35
2+
3+
feat - add SendGrid category support
4+
15
## Version 0.1.34
26

37
fixed - SendGrid v3 issues (#2020)

firestore-send-email/POSTINSTALL.md

+22
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,28 @@ admin
4040
4141
See the [official documentation](https://firebase.google.com/docs/extensions/official/firestore-send-email) for information on using this extension, including advanced use cases such as using Handlebars templates and managing email delivery status.
4242
43+
#### Firestore-Send-Email: SendGrid Categories
44+
45+
When using SendGrid (`SMTP_CONNECTION_URI` includes `sendgrid.net`), you can assign categories to your emails.
46+
47+
## Example JSON with Categories:
48+
```json
49+
{
50+
"to": ["[email protected]"],
51+
"categories": ["Example_Category"],
52+
"message": {
53+
"subject": "Test Email with Categories",
54+
"text": "This is a test email to see if categories work.",
55+
"html": "<strong>This is a test email to see if categories work.</strong>"
56+
}
57+
}
58+
```
59+
60+
Add this document to the Firestore mail collection to send categorized emails.
61+
62+
For more details, see the [SendGrid Categories documentation](https://docs.sendgrid.com/ui/sending-email/categories).
63+
64+
4365
### Monitoring
4466
4567
As a best practice, you can [monitor the activity](https://firebase.google.com/docs/extensions/manage-installed-extensions#monitor) of your installed extension, including checks on its health, usage, and logs.

firestore-send-email/PREINSTALL.md

+21
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,27 @@ You can also optionally configure this extension to render emails using [Handleb
1818

1919
When you configure this extension, you'll need to supply your **SMTP credentials for mail delivery**. Note that this extension is for use with bulk email service providers, like SendGrid, Mailgun, etc.
2020

21+
#### Firestore-Send-Email: SendGrid Categories
22+
23+
When using SendGrid (`SMTP_CONNECTION_URI` includes `sendgrid.net`), you can assign categories to your emails.
24+
25+
## Example JSON with Categories:
26+
```json
27+
{
28+
"to": ["[email protected]"],
29+
"categories": ["Example_Category"],
30+
"message": {
31+
"subject": "Test Email with Categories",
32+
"text": "This is a test email to see if categories work.",
33+
"html": "<strong>This is a test email to see if categories work.</strong>"
34+
}
35+
}
36+
```
37+
38+
Add this document to the Firestore mail collection to send categorized emails.
39+
40+
For more details, see the [SendGrid Categories documentation](https://docs.sendgrid.com/ui/sending-email/categories).
41+
2142
#### Setup Google App Passwords
2243

2344
**Google** no longer allows **Gmail** users to use their own passwords to authorize third-party apps and services. Instead, you have to use the [Sign in with App Passwords](https://support.google.com/accounts/answer/185833) service to generate a special password for each app you want to authorize. To do so:

firestore-send-email/README.md

+21
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,27 @@ You can also optionally configure this extension to render emails using [Handleb
2626

2727
When you configure this extension, you'll need to supply your **SMTP credentials for mail delivery**. Note that this extension is for use with bulk email service providers, like SendGrid, Mailgun, etc.
2828

29+
#### Firestore-Send-Email: SendGrid Categories
30+
31+
When using SendGrid (`SMTP_CONNECTION_URI` includes `sendgrid.net`), you can assign categories to your emails.
32+
33+
## Example JSON with Categories:
34+
```json
35+
{
36+
"to": ["[email protected]"],
37+
"categories": ["Example_Category"],
38+
"message": {
39+
"subject": "Test Email with Categories",
40+
"text": "This is a test email to see if categories work.",
41+
"html": "<strong>This is a test email to see if categories work.</strong>"
42+
}
43+
}
44+
```
45+
46+
Add this document to the Firestore mail collection to send categorized emails.
47+
48+
For more details, see the [SendGrid Categories documentation](https://docs.sendgrid.com/ui/sending-email/categories).
49+
2950
#### Setup Google App Passwords
3051

3152
**Google** no longer allows **Gmail** users to use their own passwords to authorize third-party apps and services. Instead, you have to use the [Sign in with App Passwords](https://support.google.com/accounts/answer/185833) service to generate a special password for each app you want to authorize. To do so:

firestore-send-email/extension.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
name: firestore-send-email
16-
version: 0.1.34
16+
version: 0.1.35
1717
specVersion: v1beta
1818

1919
displayName: Trigger Email from Firestore

firestore-send-email/functions/__tests__/helpers.test.ts

+42-45
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Mail = require("nodemailer/lib/mailer");
22
const { logger } = require("firebase-functions");
33

4-
import { setSmtpCredentials } from "../src/helpers";
4+
import { setSmtpCredentials, isSendGrid } from "../src/helpers";
55
import { Config } from "../src/types";
66

77
const consoleLogSpy = jest.spyOn(logger, "warn").mockImplementation();
@@ -11,7 +11,7 @@ const regex = new RegExp(
1111
"^(smtp[s]*://(.*?(:[^:@]*)?@)?[^:@]+:[0-9]+(\\?[^ ]*)?)$"
1212
);
1313

14-
describe("set server credentials helper function", () => {
14+
describe("setSmtpCredentials function", () => {
1515
test("return smtpServerDomain credentials with new password", () => {
1616
const config: Config = {
1717
smtpConnectionUri:
@@ -32,26 +32,7 @@ describe("set server credentials helper function", () => {
3232
expect(regex.test(config.smtpConnectionUri)).toBe(true);
3333
});
3434

35-
test("return smtpServerDomain credentials with new password (old deleted)", () => {
36-
const config: Config = {
37-
smtpConnectionUri: "smtps://[email protected]@smtp.gmail.com:465",
38-
smtpPassword: "sec#:@ret-password",
39-
location: "",
40-
mailCollection: "",
41-
defaultFrom: "",
42-
};
43-
const credentials = setSmtpCredentials(config);
44-
expect(credentials).toBeInstanceOf(Mail);
45-
expect(credentials.options.port).toBe(465);
46-
expect(credentials.options.host).toBe("smtp.gmail.com");
47-
expect(credentials.options.auth.pass).toBe(config.smtpPassword);
48-
expect(credentials.options.secure).toBe(true);
49-
50-
// The regex should match the smtpConnectionUri, it should be valid
51-
expect(regex.test(config.smtpConnectionUri)).toBe(true);
52-
});
53-
54-
test("return smtpConnectionUri credentials with old password", () => {
35+
test("return smtpServerDomain credentials with old password", () => {
5536
const config: Config = {
5637
smtpConnectionUri:
5738
@@ -130,7 +111,7 @@ describe("set server credentials helper function", () => {
130111
expect(regex.test(config.smtpConnectionUri)).toBe(true);
131112
});
132113

133-
test("return valid smtpConnectionUri credentials with special chars in password config", () => {
114+
test("return valid smtpConnectionUri credentials with valid special chars in password", () => {
134115
const config: Config = {
135116
smtpConnectionUri:
136117
"smtp://[email protected]@smtp.gmail.com:465?pool=true&service=gmail",
@@ -176,7 +157,7 @@ describe("set server credentials helper function", () => {
176157
expect(regex.test(config.smtpConnectionUri)).toBe(true);
177158
});
178159

179-
test("return invalid smtpConnectionUri credentials with invalid special chars in connectionUri password", () => {
160+
test("throw error for invalid smtpConnectionUri", () => {
180161
const config: Config = {
181162
smtpConnectionUri:
182163
"smtp://[email protected]:4,h?dhuNTbv9zMrP4&7&7%*[email protected]:465?pool=true&service=gmail",
@@ -185,48 +166,64 @@ describe("set server credentials helper function", () => {
185166
defaultFrom: "",
186167
};
187168

188-
// Expect an error to be thrown
189169
expect(() => setSmtpCredentials(config)).toThrow(Error);
190-
191170
expect(consoleLogSpy).toBeCalledWith(
192171
"Invalid URI: please reconfigure with a valid SMTP connection URI"
193172
);
194173
});
195-
test("return valid smtpConnectionUri credentials with correct seprator", () => {
174+
});
175+
176+
describe("isSendGrid function", () => {
177+
test("return true for SendGrid SMTP URI", () => {
196178
const config: Config = {
197-
smtpConnectionUri:
198-
"smtp://[email protected]:4,h?dhuNTbv9zMrP4&7&7%*[email protected]:465?pool=true&service=gmail",
179+
smtpConnectionUri: "smtps://[email protected]:465",
199180
location: "",
200181
mailCollection: "",
201182
defaultFrom: "",
202183
};
203184

204-
expect(regex.test(config.smtpConnectionUri)).toBe(true);
185+
expect(isSendGrid(config)).toBe(true);
205186
});
206187

207-
test("return invalid smtpConnectionUri credentials with invalid seprator", () => {
188+
test("return false for non-SendGrid SMTP URI", () => {
208189
const config: Config = {
209190
smtpConnectionUri:
210-
"smtp://[email protected]:4,h?dhuNTbv9zMrP4&7&7%*3:smtp.gmail.com:465?pool=true&service=gmail",
191+
"smtps://[email protected]:secret-password@smtp.gmail.com:465",
211192
location: "",
212193
mailCollection: "",
213194
defaultFrom: "",
214195
};
215196

216-
expect(regex.test(config.smtpConnectionUri)).toBe(false);
197+
expect(isSendGrid(config)).toBe(false);
217198
});
199+
});
218200

219-
/* Test removed due to the new SendGrid transporter logic - see setSendGridTransport()
220-
test("return a SendGrid transporter if the host is smtp.sendgrid.net", () => {
221-
const config: Config = {
222-
smtpConnectionUri: "smtps://apikey@smtp.sendgrid.net:465",
223-
location: "",
224-
mailCollection: "",
225-
defaultFrom: "",
226-
};
201+
test("return invalid smtpConnectionUri credentials with invalid separator", () => {
202+
const config: Config = {
203+
smtpConnectionUri:
204+
"smtp://[email protected]:4,h?dhuNTbv9zMrP4&7&7%*3:smtp.gmail.com:465?pool=true&service=gmail",
205+
location: "",
206+
mailCollection: "",
207+
defaultFrom: "",
208+
};
227209

228-
const credentials = setSmtpCredentials(config);
229-
expect(credentials.transporter.name === "nodemailer-sendgrid").toBe(true);
230-
});
231-
*/
210+
expect(regex.test(config.smtpConnectionUri)).toBe(false);
211+
});
212+
213+
test("correctly detects SendGrid SMTP URI", () => {
214+
const config: Config = {
215+
smtpConnectionUri: "smtps://[email protected]:465",
216+
location: "",
217+
mailCollection: "",
218+
defaultFrom: "",
219+
};
220+
expect(isSendGrid(config)).toBe(true);
221+
222+
const invalidConfig: Config = {
223+
smtpConnectionUri: "smtps://[email protected]:465",
224+
location: "",
225+
mailCollection: "",
226+
defaultFrom: "",
227+
};
228+
expect(isSendGrid(invalidConfig)).toBe(false);
232229
});

0 commit comments

Comments
 (0)