Skip to content

Commit 369b76b

Browse files
committed
feat: Add email validation endpoint
1 parent bf68be5 commit 369b76b

18 files changed

+3163
-5
lines changed

ModuleConfig.cfc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ component {
44
this.author = "Ortus Solutions";
55
this.webUrl = "https://github.com/coldbox-modules/sendgrid-sdk";
66
this.entrypoint = "/sendgrid";
7+
this.dependencies = [ "hyper" ];
78

89
function configure() {
910
routes = [ { pattern: "/webhooks", handler: "webhooks", action: "handle" } ];
@@ -25,4 +26,13 @@ component {
2526
};
2627
}
2728

29+
function onLoad() {
30+
binder.map( "SendGridHyperClient@sendgrid-sdk" )
31+
.to( "hyper.models.HyperBuilder" )
32+
.asSingleton()
33+
.initWith(
34+
baseURL = "https://api.sendgrid.com"
35+
);
36+
}
37+
2838
}

README.md

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,43 @@
22

33
[![Master Branch Build Status](https://img.shields.io/travis/coldbox-modules/sendgrid-sdk/master.svg?style=flat-square&label=master)](https://travis-ci.org/coldbox-modules/sendgrid-sdk)
44

5-
## An API for interacting with SendGrid, including sending emails and receiving webhooks
5+
## An API for interacting with SendGrid, including sending emails, validating email addresses, and receiving webhooks
6+
7+
### Email Validation
8+
9+
Leverage the SendGrid API to validate email addresses. SendGrid will provide
10+
a validation result, score, and test results to help you determine the validity
11+
of an email address.
12+
13+
#### Setup
14+
15+
Configure your SendGrid API key credentials in the `config/ColdBox.cfc` file.
16+
17+
Note: SendGrid uses a Bearer API token header for authentication with their API.
18+
The SendGrid API Keys can have different permissions granted and email address
19+
validation is typically seperate from all other permission sets.
20+
21+
22+
```
23+
moduleSettings = {
24+
"sendgrid-sdk" = {
25+
emailValidationAPIKey = ""
26+
}
27+
};
28+
```
29+
30+
#### Methods
31+
32+
##### validate
33+
34+
Validate the provided email address. Returns a configured `HyperRequest` instance.
35+
36+
| Name | Type | Required? | Default | Description |
37+
| -------------- | ------------- | --------- | ------- | -------------------------------------------------------------------------------- |
38+
| email | String | `true` | | The email address to validate |
39+
| source | String | `false` | | An optional text string that identifies the source of the email address |
40+
41+
642

743
### Webhooks
844

@@ -27,7 +63,7 @@ correspond with:
2763

2864
The `interceptData` is the data sent from Sendgrid.
2965

30-
### Basic Authentication
66+
#### Basic Authentication
3167

3268
SendGrid supports basic authentication when calling your webhook.
3369
To set this up, provide a `username` and `password` in your `moduleSettings`:
@@ -41,4 +77,4 @@ moduleSettings = {
4177
};
4278
```
4379

44-
Note: if you only a username or a password, `sendgrid-sdk` will return a 500 error.
80+
Note: if you only a username or a password, `sendgrid-sdk` will return a 500 error.

box.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@
1414
"location": "forgeboxStorage",
1515
"shortDescription": "An API for interacting with SendGrid, including sending emails and receiving webhooks",
1616
"description": "An API for interacting with SendGrid, including sending emails and receiving webhooks",
17-
"dependencies":{},
17+
"dependencies":{
18+
"hyper":"^3.6.2"
19+
},
1820
"devDependencies":{
1921
"coldbox":"^6.0.0",
2022
"testbox":"^4.0.0"
2123
},
2224
"installPaths":{
25+
"hyper":"modules/hyper/",
2326
"coldbox":"tests/resources/app/coldbox/",
2427
"testbox":"testbox/"
2528
},

models/SendGridClient.cfc

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Interact with the SendGrid API
3+
*/
4+
component singleton accessors="true" {
5+
6+
/**
7+
* The configured SendGrid API Key for Email Validation
8+
*/
9+
property name="emailValidationAPIKey" inject="box:setting:emailValidationAPIKey@sendgrid-sdk";
10+
11+
/**
12+
* A configured HyperBuilder client to use with the SendGrid API.
13+
* This is handled for you when using as a ColdBox module.
14+
*/
15+
property name="hyperClient" inject="SendGridHyperClient@sendgrid-sdk";
16+
17+
/**
18+
* Validate an email address
19+
*
20+
* @email The email address to validate
21+
* @source An optional text string that identifies the source of the email address
22+
*
23+
* @returns A configured HyperRequest instance.
24+
*/
25+
function validate( required string email, string source ) {
26+
var reqBody = { "email": arguments.email };
27+
if( structKeyExists( arguments, "source" ) && len( arguments.source ) ){
28+
reqBody[ "source" ] = arguments.source;
29+
}
30+
31+
return newRequest()
32+
.setUrl( "/v3/validations/email" )
33+
.setMethod( "POST" )
34+
.withHeaders( { "Authorization": "Bearer #emailValidationAPIKey#" } )
35+
.setBody( reqBody );
36+
}
37+
38+
function newRequest() {
39+
return variables.hyperClient.new();
40+
}
41+
42+
function onMissingMethod( missingMethodName, missingMethodArguments ) {
43+
return invoke( newRequest(), missingMethodName, missingMethodArguments );
44+
}
45+
46+
}

0 commit comments

Comments
 (0)