Skip to content

Commit a6bfb19

Browse files
authored
Merge pull request #2 from emailjs-com/EJS-424-sdk-v4
SDK v4
2 parents 873a336 + 9ad2822 commit a6bfb19

35 files changed

+1070
-301
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@ migrate_working_dir/
2929
.packages
3030
build/
3131
coverage/
32+
.flutter-*
33+
CRIB.md

CHANGELOG.md

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,42 @@
1+
## 4.0.0
2+
3+
- The flutter version of the SDK packages has been adjusted to global version 4. The SDK has been completely refactored. New data validation has been added to avoid wasting quota. A set of new tests has been added. Improved test code coverage.
4+
5+
- Added [blockList option](https://www.emailjs.com/docs/sdk/options/#blocklist) to block requests for unwanted variable values
6+
- Added [limitrate option](https://www.emailjs.com/docs/sdk/options/#limitrate) to install the throttle. For persistence, the shared_preferences package is used.
7+
18
## 1.3.0
2-
* Fix closed client [issue #1](https://github.com/emailjs-com/emailjs-flutter/issues/1)
9+
10+
- Fix closed client [issue #1](https://github.com/emailjs-com/emailjs-flutter/issues/1)
311

412
## 1.2.1
5-
* Fix the environment SDK version
13+
14+
- Fix the environment SDK version
615

716
## 1.2.0
8-
* Update packages
17+
18+
- Update packages
919

1020
## 1.1.0
11-
* Improve README.md
21+
22+
- Improve README.md
1223

1324
## 1.0.0
14-
* Add support for the private key
15-
* Instead of the public key argument, the settings object is passed, where the public and private keys can be set.
16-
Check out the documentation and examples.
25+
26+
- Add support for the private key
27+
- Instead of the public key argument, the settings object is passed, where the public and private keys can be set.
28+
Check out the documentation and examples.
1729

1830
## 0.0.3
19-
* re-format examples code
31+
32+
- re-format examples code
2033

2134
## 0.0.2
2235

23-
* Score fixes
24-
* Added examples
25-
* Dart formatter
36+
- Score fixes
37+
- Added examples
38+
- Dart formatter
2639

2740
## 0.0.1
2841

29-
* Official EmailJS SDK for Flutter
42+
- Official EmailJS SDK for Flutter

README.md

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Use you EmailJS account for sending emails.
99
This is a flutter-only version, otherwise use
1010
- [Browser SDK](https://www.npmjs.com/package/@emailjs/browser)
1111
- [Node.js SDK](https://www.npmjs.com/package/@emailjs/nodejs)
12+
- [React Native SDK](https://www.npmjs.com/package/@emailjs/react-native)
1213
- [REST API](https://www.emailjs.com/docs/rest-api/send/)
1314

1415
## Links
@@ -18,7 +19,7 @@ This is a flutter-only version, otherwise use
1819
## Intro
1920

2021
EmailJS helps to send emails directly from your code.
21-
No large knowledge is required – just connect EmailJS to one of the supported
22+
No server is required – just connect EmailJS to one of the supported
2223
email services, create an email template, and use our SDK
2324
to trigger an email.
2425

@@ -44,47 +45,47 @@ through [Account:Security](https://dashboard.emailjs.com/admin/account/security)
4445
**send email**
4546

4647
```dart
47-
import package:emailjs/emailjs.dart
48+
import package:emailjs/emailjs.dart as emailjs
4849
4950
Map<String, dynamic> templateParams = {
5051
'name': 'James',
5152
'notes': 'Check this out!'
5253
};
5354
5455
try {
55-
await EmailJS.send(
56-
'<YOUR_SERVICE_ID>',
57-
'<YOUR_TEMPLATE_ID>',
56+
await emailjs.send(
57+
'YOUR_SERVICE_ID',
58+
'YOUR_TEMPLATE_ID',
5859
templateParams,
59-
const Options(
60-
publicKey: '<YOUR_PUBLIC_KEY>',
61-
privateKey: '<YOUR_PRIVATE_KEY>',
60+
const emailjs.Options(
61+
publicKey: 'YOUR_PUBLIC_KEY',
62+
privateKey: 'YOUR_PRIVATE_KEY',
6263
),
6364
);
6465
print('SUCCESS!');
6566
} catch (error) {
66-
print(error.toString());
67+
print('$error');
6768
}
6869
```
6970

7071
**init (optional)**
7172
```dart
72-
import package:emailjs/emailjs.dart
73+
import package:emailjs/emailjs.dart as emailjs
7374
7475
// set Public Key as global settings
75-
EmailJS.init(const Options(
76-
publicKey: '<YOUR_PUBLIC_KEY>',
77-
privateKey: '<YOUR_PRIVATE_KEY>',
76+
emailjs.init(const emailjs.Options(
77+
publicKey: 'YOUR_PUBLIC_KEY',
78+
privateKey: 'YOUR_PRIVATE_KEY',
7879
));
7980
8081
try {
8182
// send the email without dynamic variables
82-
await EmailJS.send(
83-
'<YOUR_SERVICE_ID>',
84-
'<YOUR_TEMPLATE_ID>',
83+
await emailjs.send(
84+
'YOUR_SERVICE_ID',
85+
'YOUR_TEMPLATE_ID',
8586
);
8687
print('SUCCESS!');
8788
} catch (error) {
88-
print(error.toString());
89+
print('$error');
8990
}
9091
```

example/lib/main.dart

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import 'package:flutter/material.dart';
2-
import 'package:emailjs/emailjs.dart';
2+
import 'package:emailjs/emailjs.dart' as emailjs;
33

44
void main() {
55
runApp(const App());
@@ -31,22 +31,25 @@ class HomePage extends StatefulWidget {
3131
class _HomePageState extends State<HomePage> {
3232
void _sendEmail() async {
3333
try {
34-
await EmailJS.send(
35-
'<YOUR_SERVICE_ID>',
36-
'<YOUR_TEMPLATE_ID>',
34+
await emailjs.send(
35+
'YOUR_SERVICE_ID',
36+
'YOUR_TEMPLATE_ID',
3737
{
38-
'user_email': '[email protected]',
38+
'to_email': '[email protected]',
3939
'message': 'Hi',
4040
},
41-
const Options(
42-
publicKey: '<YOUR_PUBLIC_KEY>',
43-
privateKey: '<YOUR_PRIVATE_KEY>',
44-
),
41+
const emailjs.Options(
42+
publicKey: 'YOUR_PUBLIC_KEY',
43+
privateKey: 'YOUR_PRIVATE_KEY',
44+
limitRate: const emailjs.LimitRate(
45+
id: 'app',
46+
throttle: 10000,
47+
)),
4548
);
4649
print('SUCCESS!');
4750
} catch (error) {
48-
if (error is EmailJSResponseStatus) {
49-
print('ERROR... ${error.status}: ${error.text}');
51+
if (error is emailjs.EmailJSResponseStatus) {
52+
print('ERROR... $error');
5053
}
5154
print(error.toString());
5255
}

lib/emailjs.dart

Lines changed: 5 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,9 @@
11
library emailjs;
22

3-
import 'dart:convert';
4-
import 'package:http/http.dart' as http;
5-
6-
import 'src/models/emailjs_response_status.dart';
7-
import 'src/utils/validate_params.dart';
8-
import 'src/api/send_json.dart';
9-
import 'src/models/options.dart';
10-
113
export 'src/models/emailjs_response_status.dart';
4+
export 'src/models/storage_provider.dart';
125
export 'src/models/options.dart';
13-
14-
class EmailJS {
15-
/// Public Key specified in the [init] method
16-
static String _publicKey = '';
17-
18-
/// Private Key specified in the [init] method
19-
static String? _privateKey;
20-
21-
/// API host specified in the [init] method
22-
static String _host = 'api.emailjs.com';
23-
24-
/// HTTP Client specified in the [init] method
25-
static http.Client? _httpClient;
26-
27-
/// Global configuration for EmailJS
28-
///
29-
/// Sets globally the EmailJS [options]
30-
static void init(
31-
Options options, [
32-
String? host,
33-
http.Client? customHttpClient,
34-
]) {
35-
EmailJS._publicKey = options.publicKey;
36-
EmailJS._privateKey = options.privateKey;
37-
EmailJS._host = host ?? 'api.emailjs.com';
38-
EmailJS._httpClient = customHttpClient;
39-
}
40-
41-
/// Sends the email through the [serviceID] using the ready-made [templateID].
42-
///
43-
/// It's possible to pass [templatePrams] dynamic variables,
44-
/// and set the [options] for this call.
45-
static Future<EmailJSResponseStatus> send(
46-
String serviceID,
47-
String templateID, [
48-
Map<String, dynamic>? templatePrams,
49-
Options? options,
50-
]) async {
51-
final pubKey = options?.publicKey ?? EmailJS._publicKey;
52-
final prKey = options?.privateKey ?? EmailJS._privateKey;
53-
54-
validateParams(pubKey, serviceID, templateID);
55-
56-
final Map<String, dynamic> params = {
57-
'lib_version': '1.3.0',
58-
'user_id': pubKey,
59-
'accessToken': prKey,
60-
'service_id': serviceID,
61-
'template_id': templateID,
62-
'template_params': templatePrams,
63-
};
64-
65-
return await sendJSON(
66-
Uri.https(EmailJS._host, 'api/v1.0/email/send'),
67-
json.encode(params),
68-
EmailJS._httpClient,
69-
);
70-
}
71-
}
6+
export 'src/models/block_list.dart';
7+
export 'src/models/limit_rate.dart';
8+
export 'src/methods/init.dart';
9+
export 'src/methods/send.dart';

lib/src/api/send_json.dart

Lines changed: 0 additions & 32 deletions
This file was deleted.

lib/src/api/send_post.dart

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import 'package:http/http.dart';
2+
import '../store/store.dart';
3+
import '../models/emailjs_response_status.dart';
4+
5+
/// sends JSON object via HTTPS POST
6+
Future<EmailJSResponseStatus> sendPost(
7+
String url,
8+
String data, [
9+
Client? client,
10+
]) async {
11+
client ??= Client();
12+
13+
try {
14+
final response = await client.post(
15+
Uri.https(store.host, url),
16+
headers: {
17+
'Content-Type': 'application/json',
18+
},
19+
body: data,
20+
);
21+
22+
final responseStatus = EmailJSResponseStatus(
23+
status: response.statusCode,
24+
text: response.body,
25+
);
26+
27+
if (response.statusCode == 200) {
28+
return responseStatus;
29+
} else {
30+
throw responseStatus;
31+
}
32+
} finally {
33+
client.close();
34+
}
35+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import '../models/emailjs_response_status.dart';
2+
3+
EmailJSResponseStatus blockedEmailError() {
4+
return const EmailJSResponseStatus(
5+
status: 403,
6+
text: 'Forbidden',
7+
);
8+
}

lib/src/errors/limit_rate_error.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import '../models/emailjs_response_status.dart';
2+
3+
EmailJSResponseStatus limitRateError() {
4+
return const EmailJSResponseStatus(
5+
status: 429,
6+
text: 'Too Many Requests',
7+
);
8+
}

lib/src/methods/init.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import 'package:http/http.dart';
2+
import '../models/options.dart';
3+
import '../store/store.dart';
4+
5+
/// Global configuration for EmailJS
6+
///
7+
/// Sets globally the EmailJS [options]
8+
void init(Options options, [Client? client]) {
9+
store.publicKey = options.publicKey;
10+
store.privateKey = options.privateKey;
11+
store.blockList = options.blockList;
12+
store.limitRate = options.limitRate;
13+
store.storageProvider = options.storageProvider;
14+
store.origin = options.origin;
15+
16+
// for testing porpose
17+
store.client = client;
18+
}

0 commit comments

Comments
 (0)