Spaces:
Running
Running
File size: 6,460 Bytes
5c05829 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | # Official EmailJS SDK for Browsers
SDK for [EmailJS.com](https://www.emailjs.com) customers.
\
Use your EmailJS account for sending emails.
[](https://codecov.io/gh/emailjs-com/emailjs-sdk)
[](https://www.npmjs.com/package/@emailjs/browser)
## Disclaimer
This is a browser platform, otherwise use
- [Node.js](https://www.npmjs.com/package/@emailjs/nodejs)
- [React Native](https://www.npmjs.com/package/@emailjs/react-native)
- [Flutter](https://pub.dev/packages/emailjs)
- [REST API](https://www.emailjs.com/docs/rest-api/send/)
## Links
[Official SDK Docs](https://www.emailjs.com/docs)
## Intro
EmailJS helps you send emails directly from code with one command.
No server is required – just connect EmailJS to one of the supported
email services, create an email template, and use our SDK
to trigger an email.
## Usage
Install EmailJS SDK using [npm](https://www.npmjs.com/):
```bash
$ npm install @emailjs/browser
```
Or manually:
```html
<script
type="text/javascript"
src="https://cdn.jsdelivr.net/npm/@emailjs/browser@4/dist/email.min.js">
</script>
<script type="text/javascript">
(function () {
emailjs.init({
publicKey: 'YOUR_PUBLIC_KEY',
});
})();
</script>
```
## Examples
**Send the email using the customized send method**
```js
var templateParams = {
name: 'James',
notes: 'Check this out!',
};
emailjs.send('YOUR_SERVICE_ID', 'YOUR_TEMPLATE_ID', templateParams).then(
function (response) {
console.log('SUCCESS!', response.status, response.text);
},
function (err) {
console.log('FAILED...', err);
},
);
```
**Send the email from a form using the sendForm method**
```js
emailjs.sendForm('YOUR_SERVICE_ID', 'YOUR_TEMPLATE_ID', '#myForm').then(
function (response) {
console.log('SUCCESS!', response.status, response.text);
},
function (err) {
console.log('FAILED...', err);
},
);
```
**Using Angular / VueJS / ReactJS / Svelte / any other modern framework**
```js
import emailjs from '@emailjs/browser';
const templateParams = {
name: 'James',
notes: 'Check this out!',
};
emailjs
.send('YOUR_SERVICE_ID', 'YOUR_TEMPLATE_ID', templateParams, {
publicKey: 'YOUR_PUBLIC_KEY',
})
.then(
(response) => {
console.log('SUCCESS!', response.status, response.text);
},
(err) => {
console.log('FAILED...', err);
},
);
```
**await/async with EmailJS error handler**
```js
import emailjs, { EmailJSResponseStatus } from '@emailjs/browser';
try {
await emailjs.send(
'YOUR_SERVICE_ID',
'YOUR_TEMPLATE_ID',
{},
{
publicKey: 'YOUR_PUBLIC_KEY',
},
);
console.log('SUCCESS!');
} catch (err) {
if (err instanceof EmailJSResponseStatus) {
console.log('EMAILJS FAILED...', err);
return;
}
console.log('ERROR', err);
}
```
## Configuration
**Options**
Options can be declared globally using the **init** method or locally as the fourth parameter of a function.
\
The local parameter will have higher priority than the global one.
| Name | Type | Default | Description |
| --------------- | --------------- | ------- | -------------------------------------------------------- |
| publicKey | String | | The public key is required to invoke the method. |
| blockHeadless | Boolean | False | Method will return error 451 if the browser is headless. |
| blockList | BlockList | | Block list settings. |
| limitRate | LimitRate | | Limit rate configuration. |
| storageProvider | StorageProvider | | Provider for a custom key-value storage. |
**BlockList**
Allows to ignore a method call if the watched variable contains a value from the block list.
\
The method will return the error 403 if the request is blocked.
| Name | Type | Description |
| ------------- | -------- | -------------------------------------------------- |
| list | String[] | The array of strings contains values for blocking. |
| watchVariable | String | A name of the variable to be watched. |
**LimitRate**
Allows to set the limit rate for calling a method.
\
If the request hits the limit rate, the method will return the error 429.
| Name | Type | Default | Description |
| -------- | ------ | --------- | ---------------------------------------------------------------------------------------------------------------------------------------- |
| id | String | page path | The limit rate is per page by default. To override the behavior, set the ID. It can be a custom ID for each page, group, or application. |
| throttle | Number | | _(ms)_ After how many milliseconds a next request is allowed. |
**StorageProvider**
Allows to provide a custom key value storage. By default, localStorage is used if available.
\
The custom provider must match the interface.
```ts
interface StorageProvider {
get: (key: string) => Promise<string | null | undefined>;
set: (key: string, value: string) => Promise<void>;
remove: (key: string) => Promise<void>;
}
```
**Declare global settings**
```js
import emailjs from '@emailjs/browser';
emailjs.init({
publicKey: 'YOUR_PUBLIC_KEY',
blockHeadless: true,
blockList: {
list: ['foo@emailjs.com', 'bar@emailjs.com'],
},
limitRate: {
throttle: 10000, // 10s
},
});
```
**Overwrite settings locally**
```js
import emailjs from '@emailjs/browser';
const templateParams = {
name: 'James',
notes: 'Check this out!',
};
emailjs
.send('YOUR_SERVICE_ID', 'YOUR_TEMPLATE_ID', templateParams, {
publicKey: 'YOUR_PUBLIC_KEY',
blockList: {
watchVariable: 'userEmail',
},
limitRate: {
throttle: 0, // turn off the limit rate for these requests
},
})
.then(
(response) => {
console.log('SUCCESS!', response.status, response.text);
},
(err) => {
console.log('FAILED...', err);
},
);
```
|