UNPKG

5.23 kBMarkdownView Raw
1
2[mailjet]: http://www.mailjet.com
3[api_credential]: https://app.mailjet.com/account/api_keys
4[eventemitter]: https://nodejs.org/api/events.html
5[doc]: http://dev.mailjet.com/guides/?javascript#
6[api_doc_repo]: https://github.com/mailjet/api-documentation
7
8![alt text](http://cdn.appstorm.net/web.appstorm.net/files/2012/02/mailjet_logo_200x200.png "Mailjet")
9
10
11[![Build Status](https://travis-ci.org/mailjet/mailjet-apiv3-nodejs.svg?branch=master)](https://travis-ci.org/mailjet/mailjet-apiv3-nodejs)
12# Mailjet NodeJs Wrapper
13
14Please welcome the new [Mailjet][mailjet] official NodeJS API wrapper!
15
16[Mailjet][mailjet] is an Email Service Provider (ESP). Visit the website and get comfortable!
17
18Every code examples can be find on the [Mailjet Documentation][doc]
19
20(Please refer to the [Mailjet Documentation Repository][api_doc_repo] to contribute to the documentation examples)
21
22
23## Getting started
24
25first, create a project folder
26
27`mkdir mailjet-project && cd $_`
28
29### Installation
30
31if you want to get a global installation, you can add `-g`
32
33`npm install node-mailjet`
34
35
36
37### Show me the code
38
39To authenticate, go get your API key, and API secret [here][api_credential],
40open your favorite text editor and import the mailjet module
41
42``` javascript
43
44var Mailjet = require('node-mailjet').connect('api key', 'api secret');
45
46```
47
48### Get cosy with Mailjet
49
50
51#### Save your `API_KEY` and `API_SECRET`:
52
53`echo 'export MJ_APIKEY_PUBLIC=MY_API_KEY' >> ~/.zshrc`
54
55`echo 'export MJ_APIKEY_PRIVATE=MY_API_SECRET' >> ~/.zshrc`
56
57`source ~/.zshrc`
58
59replace `zshrc` with `bash_profile` if you are simply using bash
60
61#### And use it in your projects
62
63``` javascript
64
65var apiKey = process.env.MJ_APIKEY_PUBLIC,
66 apiSecret = process.env.MJ_APIKEY_PRIVATE;
67
68```
69
70### Store a Mailjet resource
71
72``` javascript
73
74// GET resource
75var user = Mailjet.get('user');
76
77// POST resource
78var sender = Mailjet.post('sender');
79
80```
81
82### Request your resource with a callback function
83
84``` javascript
85
86user.request(function (error, response, body) {
87 if (error)
88 console.log ('Oops, something went wrong ' + response.statusCode);
89 else
90 console.log (body);
91});
92
93```
94
95### Make the same request with a Promise
96
97``` javascript
98
99user.request()
100 .then(function (result) {
101 // do something with the result
102 // result structure is {response: {...}, body: {...}}
103 })
104 .catch(function (reason) {
105 // handle the rejection reason
106 console.log(reason.statusCode)
107 })
108
109```
110
111### Pass data to your requests
112
113``` javascript
114
115sender.request({ Email: 'mr@mailjet.com' })
116 .then(handleData)
117 .catch(handleError);
118
119```
120
121### Pass parameters as well as a callback
122
123``` javascript
124
125var getContacts = Mailjet.get('contact');
126
127getContacts.request({Limit: 3}, handleContacts);
128
129```
130
131### Request a resource with an ID
132
133``` javascript
134
135getContacts.id(2).request(handleSingleContact)
136
137````
138
139### Request a ressource with an Action
140
141``` javascript
142
143var postContact = Mailjet.post('contact');
144
145postContact.action('managemanycontacts').request({
146 ContactLists: MyContactListsArray,
147 Contacts: MyContactsArray,
148}, handlePostResponse)
149
150```
151
152### Send an Email
153
154``` javascript
155
156var sendEmail = Mailjet.post('send');
157
158var emailData = {
159 'FromEmail': 'my@email.com',
160 'FromName': 'My Name',
161 'Subject': 'Test with the NodeJS Mailjet wrapper',
162 'Text-part': 'Hello NodeJs !',
163 'Recipients': [{'Email': 'roger@smith.com'}],
164 'Attachments': [{
165 "Content-Type": "text-plain",
166 "Filename": "test.txt",
167 "Content": "VGhpcyBpcyB5b3VyIGF0dGFjaGVkIGZpbGUhISEK",
168 }],
169}
170
171sendEmail
172 .request(emailData)
173 .then(handlePostResponse)
174 .catch(handleError);
175
176```
177
178### Send two Emails
179
180``` javascript
181
182var emailData = {
183 'FromEmail': 'gbadi@student.42.fr',
184 'FromName': 'Guillaume badi',
185 'Subject': 'Coucou Mailjet2',
186 'Text-part': 'Hello World2',
187 'Recipients': [{'Email': 'gbadi@mailjet.com'}],
188}
189
190var emailData2 = emailData;
191emailData2['Text-part'] = 'This is another Email';
192
193sendEmail
194 .request(emailData)
195 .then(handleData)
196 .catch(handleError);
197
198sendEmail
199 .request(emailData2)
200 .then(handleData)
201 .catch(handleError);
202
203```
204## Have Fun !
205``` javascript
206var mailjet = require ('./mailjet-client')
207 .connect(process.env.MJ_APIKEY_PUBLIC, process.env.MJ_APIKEY_PRIVATE)
208
209function handleError (err) {
210 throw new Error(err.ErrorMessage);
211}
212
213function newContact (email) {
214 mailjet.post('contact')
215 .request({Email: email})
216 .catch(handleError);
217}
218
219function testEmail (text) {
220 email = {};
221 email['FromName'] = 'Your Name';
222 email['FromEmail'] = 'Your Sender Adress';
223 email['Subject'] = 'Test Email';
224 email['Recipients'] = [{Email: 'Your email'}];
225 email['Text-Part'] = text;
226
227 mailjet.post('send')
228 .request(email)
229 .catch(handleError);
230}
231
232testEmail('Hello World!');
233```
234
235
236## Run Test
237
238``` bash
239npm test
240```
241
242## Contribute
243
244Mailjet loves developers. You can be part of this project!
245
246This wrapper is a great introduction to the open source world, check out the code!
247
248Feel free to ask anything, and contribute:
249
250- Fork the project.
251- Create a new branch.
252- Implement your feature or bug fix.
253- Add documentation to it.
254- Commit, push, open a pull request and voila.
255
256TODO:
257
258- Extend Error class to create Api errors