Skip to content

Commit f74775e

Browse files
chore: added examples for email channels (#199)
Co-authored-by: Karl Lingiah <karl@superchilled.co.uk>
1 parent ed20cf9 commit f74775e

5 files changed

Lines changed: 2474 additions & 795 deletions

File tree

.env-example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ MMS_SENDER_ID=
5555
## For RCS messaging
5656
RCS_SENDER_ID=
5757

58+
## For Email messaging
59+
MESSAGES_TO_EMAIL=
60+
EMAIL_SENDER_ID=
61+
5862
## For WhatsApp messaging
5963
WHATSAPP_CATALOG_ID=
6064
WHATSAPP_OTP=
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
require('dotenv').config({ path: __dirname + '/../../.env' });
2+
3+
const VONAGE_APPLICATION_ID = process.env.VONAGE_APPLICATION_ID;
4+
const VONAGE_PRIVATE_KEY = process.env.VONAGE_PRIVATE_KEY;
5+
const MESSAGES_TO_EMAIL = process.env.MESSAGES_TO_EMAIL;
6+
const EMAIL_SENDER_ID = process.env.EMAIL_SENDER_ID;
7+
const MESSAGES_API_URL = process.env.MESSAGES_API_URL;
8+
9+
const { Vonage } = require('@vonage/server-sdk');
10+
const { Channels, MessageType } = require('@vonage/messages');
11+
12+
/**
13+
* It is best to send messages using JWT instead of basic auth. If you leave out
14+
* apiKey and apiSecret, the messages SDK will send requests using JWT tokens
15+
*
16+
* @link https://developer.vonage.com/en/messages/technical-details#authentication
17+
*/
18+
const vonage = new Vonage(
19+
{
20+
applicationId: VONAGE_APPLICATION_ID,
21+
privateKey: VONAGE_PRIVATE_KEY,
22+
},
23+
{
24+
...(MESSAGES_API_URL ? { apiHost: MESSAGES_API_URL } : {}),
25+
},
26+
);
27+
28+
vonage.messages.send({
29+
to: MESSAGES_TO_EMAIL,
30+
from: EMAIL_SENDER_ID,
31+
messageType: MessageType.CONTENT,
32+
channel: Channels.EMAIL,
33+
content: [
34+
{
35+
type: 'text',
36+
text: 'This is the plain-text body.'
37+
},
38+
{
39+
type: 'html',
40+
body: '<p>This is the <strong>HTML</strong> body.</p>'
41+
}
42+
],
43+
email: {
44+
subject: 'Your content message subject'
45+
}
46+
})
47+
.then(({ messageUUID }) => console.log(messageUUID))
48+
.catch((error) => console.error(error));

messages/email/send-email-html.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
require('dotenv').config({ path: __dirname + '/../../.env' });
2+
3+
const VONAGE_APPLICATION_ID = process.env.VONAGE_APPLICATION_ID;
4+
const VONAGE_PRIVATE_KEY = process.env.VONAGE_PRIVATE_KEY;
5+
const MESSAGES_TO_EMAIL = process.env.MESSAGES_TO_EMAIL;
6+
const EMAIL_SENDER_ID = process.env.EMAIL_SENDER_ID;
7+
const MESSAGES_API_URL = process.env.MESSAGES_API_URL;
8+
9+
const { Vonage } = require('@vonage/server-sdk');
10+
const { Channels, MessageType } = require('@vonage/messages');
11+
12+
/**
13+
* It is best to send messages using JWT instead of basic auth. If you leave out
14+
* apiKey and apiSecret, the messages SDK will send requests using JWT tokens
15+
*
16+
* @link https://developer.vonage.com/en/messages/technical-details#authentication
17+
*/
18+
const vonage = new Vonage(
19+
{
20+
applicationId: VONAGE_APPLICATION_ID,
21+
privateKey: VONAGE_PRIVATE_KEY,
22+
},
23+
{
24+
...(MESSAGES_API_URL ? { apiHost: MESSAGES_API_URL } : {}),
25+
},
26+
);
27+
28+
vonage.messages.send({
29+
to: MESSAGES_TO_EMAIL,
30+
from: EMAIL_SENDER_ID,
31+
messageType: MessageType.HTML,
32+
channel: Channels.EMAIL,
33+
html: {
34+
body: '<h1>Hello from Vonage</h1><p>This is an Email HTML message sent via the Vonage Messages API.</p>'
35+
},
36+
email: {
37+
subject: 'Your HTML message subject'
38+
}
39+
})
40+
.then(({ messageUUID }) => console.log(messageUUID))
41+
.catch((error) => console.error(error));

messages/email/send-email-text.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require('dotenv').config({ path: __dirname + '/../../.env' });
2+
3+
const VONAGE_APPLICATION_ID = process.env.VONAGE_APPLICATION_ID;
4+
const VONAGE_PRIVATE_KEY = process.env.VONAGE_PRIVATE_KEY;
5+
const MESSAGES_TO_EMAIL = process.env.MESSAGES_TO_EMAIL;
6+
const EMAIL_SENDER_ID = process.env.EMAIL_SENDER_ID;
7+
const MESSAGES_API_URL = process.env.MESSAGES_API_URL;
8+
9+
const { Vonage } = require('@vonage/server-sdk');
10+
const { Channels, MessageType } = require('@vonage/messages');
11+
12+
/**
13+
* It is best to send messages using JWT instead of basic auth. If you leave out
14+
* apiKey and apiSecret, the messages SDK will send requests using JWT tokens
15+
*
16+
* @link https://developer.vonage.com/en/messages/technical-details#authentication
17+
*/
18+
const vonage = new Vonage(
19+
{
20+
applicationId: VONAGE_APPLICATION_ID,
21+
privateKey: VONAGE_PRIVATE_KEY,
22+
},
23+
{
24+
...(MESSAGES_API_URL ? { apiHost: MESSAGES_API_URL } : {}),
25+
},
26+
);
27+
28+
vonage.messages.send({
29+
to: MESSAGES_TO_EMAIL,
30+
from: EMAIL_SENDER_ID,
31+
messageType: MessageType.HTML,
32+
channel: Channels.EMAIL,
33+
text: 'This is an Email text message sent via the Vonage Messages API.',
34+
email: {
35+
subject: 'Your message subject'
36+
}
37+
})
38+
.then(({ messageUUID }) => console.log(messageUUID))
39+
.catch((error) => console.error(error));

0 commit comments

Comments
 (0)