|
| 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)); |
0 commit comments