-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6cf29da
commit d4282aa
Showing
30 changed files
with
424 additions
and
21 deletions.
There are no files selected for viewing
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
83 changes: 83 additions & 0 deletions
83
examples/simple-examples/src/mailgun/emails/sendEmailTemplate.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { | ||
getMailgunDomainFromConfig, | ||
getMailgunRecipientFromConfig, | ||
getMailgunSenderFromConfig, getMailgunTemplateNameFromConfig, | ||
initMailgunService, | ||
printFullResponse, | ||
} from '../../config'; | ||
import { Mailgun } from '@sinch/sdk-core'; | ||
import { createReadStream, promises as fsPromises } from 'fs'; | ||
import path from 'path'; | ||
|
||
(async () => { | ||
console.log('*************'); | ||
console.log('* SendEmail *'); | ||
console.log('*************'); | ||
|
||
const domainName = getMailgunDomainFromConfig(); | ||
const sender = getMailgunSenderFromConfig(); | ||
const recipient = getMailgunRecipientFromConfig(); | ||
const templateName = getMailgunTemplateNameFromConfig(); | ||
|
||
const sinchLogo: Mailgun.EmailAttachment = { | ||
filename: 'logo.png', | ||
data: await fsPromises.readFile(path.join(__dirname, '..', 'attachments', 'sinch-logo.png')), | ||
}; | ||
|
||
const receipt: Mailgun.EmailAttachment = { | ||
filename: 'receipt.txt', | ||
data: 'This is your receipt', | ||
}; | ||
|
||
const requestData: Mailgun.SendEmailRequest = { | ||
from: sender, | ||
to: recipient, | ||
subject: 'First email from the Node.js SDK', | ||
template: templateName, | ||
templateProperties: { | ||
version: 'initial', | ||
variables: { | ||
param: 'The Gourmet Whale', | ||
orders: [ | ||
{ | ||
id: 123, | ||
name: 'Dark Chocolate Box', | ||
}, | ||
{ | ||
id: 456, | ||
name: 'Calissons x12', | ||
}, | ||
], | ||
}, | ||
}, | ||
inline: sinchLogo, | ||
attachment: receipt, | ||
text: 'Fallback text for text-only email clients', | ||
recipientVariables: { | ||
[recipient]: { | ||
name: 'John', | ||
}, | ||
}, | ||
overrideProperties: { | ||
tag: ['test', 'sdk-node'], | ||
}, | ||
customHeaders: { | ||
'X-sent-by': 'Node.js SDK', | ||
}, | ||
customVariables: { | ||
'message_id': 123, | ||
}, | ||
}; | ||
|
||
const mailgunService = initMailgunService(); | ||
let response; | ||
try { | ||
response = await mailgunService.emails.sendEmail(domainName, requestData); | ||
} catch (error) { | ||
console.error('Error when sending a message'); | ||
throw error; | ||
} | ||
|
||
printFullResponse(response); | ||
|
||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/mailgun/src/models/v1/emails/request/send-email-html-in-template-request/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type { SendEmailHtmlInTemplateRequest } from './send-email-html-in-template-request'; |
61 changes: 61 additions & 0 deletions
61
...uest/send-email-html-in-template-request/send-email-html-in-template-request.transform.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { SendEmailHtmlInTemplateRequest } from './send-email-html-in-template-request'; | ||
import { appendArrayToFormData, appendCustomDataToFormData, appendSerializedMapToFormData } from '../helpers'; | ||
import { appendOverridePropertiesToFormData } from '../override-properties/override-properties.transform'; | ||
import { appendTemplatePropertiesToFormData } from '../template-properties/template-properties.transform'; | ||
import FormData = require('form-data'); | ||
|
||
// eslint-disable-next-line valid-jsdoc | ||
/** | ||
* ** INTERNAL METHOD ** IT SHOULD NOT BE USED DIRECTLY BY SDK USERS AS IT CAN BE REMOVED OR MODIFIED WITHOUT NOTICE | ||
*/ | ||
export const transformSendEmailHtmlInTemplateRequestIntoApiRequestBody = ( | ||
sdkRequest: SendEmailHtmlInTemplateRequest, | ||
) => { | ||
const formData = new FormData(); | ||
if (sdkRequest['to'] != null) { | ||
appendArrayToFormData(sdkRequest['to'], 'to', formData); | ||
} | ||
if (sdkRequest['from'] != null) { | ||
formData.append('from', sdkRequest['from']); | ||
} | ||
if (sdkRequest['cc'] != null) { | ||
appendArrayToFormData(sdkRequest['cc'], 'cc', formData); | ||
} | ||
if (sdkRequest['bcc'] != null) { | ||
appendArrayToFormData(sdkRequest['bcc'], 'bcc', formData); | ||
} | ||
if (sdkRequest['subject'] != null) { | ||
formData.append('subject', sdkRequest['subject']); | ||
} | ||
if (sdkRequest['text'] != null) { | ||
formData.append('text', sdkRequest['text']); | ||
} | ||
if (sdkRequest['ampHtml'] != null) { | ||
formData.append('amp-html', sdkRequest['ampHtml']); | ||
} | ||
if (sdkRequest['attachment'] != null) { | ||
appendArrayToFormData(sdkRequest['attachment'], 'attachment', formData); | ||
} | ||
if (sdkRequest['inline'] != null) { | ||
appendArrayToFormData(sdkRequest['inline'], 'inline', formData); | ||
} | ||
if (sdkRequest['recipientVariables'] != null) { | ||
appendSerializedMapToFormData(sdkRequest['recipientVariables'], 'recipient-variables', formData); | ||
} | ||
if (sdkRequest['overrideProperties'] != null) { | ||
appendOverridePropertiesToFormData(sdkRequest['overrideProperties'], formData); | ||
} | ||
if (sdkRequest['customVariables'] != null) { | ||
appendCustomDataToFormData(sdkRequest['customVariables'], 'v:', formData); | ||
} | ||
if (sdkRequest['customHeaders'] != null) { | ||
appendCustomDataToFormData(sdkRequest['customHeaders'], 'h:', formData); | ||
} | ||
if (sdkRequest['template'] != null) { | ||
formData.append('template', sdkRequest['template']); | ||
} | ||
if (sdkRequest['templateProperties'] != null) { | ||
appendTemplatePropertiesToFormData(sdkRequest['templateProperties'], formData); | ||
} | ||
return formData; | ||
}; |
38 changes: 38 additions & 0 deletions
38
...emails/request/send-email-html-in-template-request/send-email-html-in-template-request.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { OverrideProperties } from '../override-properties'; | ||
import { TemplateProperties } from '../template-properties'; | ||
import { EmailAttachment } from '../email-attachment'; | ||
|
||
export interface SendEmailHtmlInTemplateRequest { | ||
/** Email address of the recipient(s). Example: `\"Bob <[email protected]>\"`. You can use commas to separate multiple recipients */ | ||
to: string | string[]; | ||
/** Email address for `From` header */ | ||
from: string; | ||
/** Same as `To` but for `Cc` */ | ||
cc?: string | string[]; | ||
/** Same as `To` but for `Bcc` */ | ||
bcc?: string | string[]; | ||
/** Message subject */ | ||
subject: string; | ||
/** Body of the message (text version) */ | ||
text?: string; | ||
/** AMP part of the message. Please follow Google guidelines to compose and send AMP emails */ | ||
ampHtml?: string; | ||
/** File attachment. You can post multiple `attachment` values. **Important:** You must use `multipart/form-data` encoding for sending attachments */ | ||
attachment?: EmailAttachment; | ||
/** Attachment with `inline` disposition. Can be used to send inline images (see example). You can post multiple `inline` values */ | ||
inline?: EmailAttachment; | ||
/** A valid JSON-encoded dictionary, where key is a plain recipient address and value is a dictionary with variables that can be referenced in the message body. See <strong>Batch Sending</strong> for more information */ | ||
recipientVariables?: { | ||
[key: string]: Record<string, string | number | boolean | Date>; | ||
}; | ||
/** @see OverrideProperties */ | ||
overrideProperties?: OverrideProperties; | ||
/** List of */ | ||
customVariables?: Record<string, string | number | boolean | Date>; | ||
/** List of */ | ||
customHeaders?: Record<string, string | number | boolean | Date>; | ||
/** Name of a template stored via template API to use to render the email body. See **Templates** for more information */ | ||
template?: string; | ||
/** @see TemplateProperties */ | ||
templateProperties?: TemplateProperties; | ||
} |
1 change: 1 addition & 0 deletions
1
packages/mailgun/src/models/v1/emails/request/send-email-html-inline-request/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type { SendEmailHtmlInlineRequest } from './send-email-html-inline-request'; |
55 changes: 55 additions & 0 deletions
55
...emails/request/send-email-html-inline-request/send-email-html-inline-request.transform.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { SendEmailHtmlInlineRequest } from './send-email-html-inline-request'; | ||
import { appendArrayToFormData, appendCustomDataToFormData, appendSerializedMapToFormData } from '../helpers'; | ||
import { appendOverridePropertiesToFormData } from '../override-properties/override-properties.transform'; | ||
import FormData = require('form-data'); | ||
|
||
// eslint-disable-next-line valid-jsdoc | ||
/** | ||
* ** INTERNAL METHOD ** IT SHOULD NOT BE USED DIRECTLY BY SDK USERS AS IT CAN BE REMOVED OR MODIFIED WITHOUT NOTICE | ||
*/ | ||
export const transformSendEmailHtmlInlineRequestIntoApiRequestBody = (sdkRequest: SendEmailHtmlInlineRequest) => { | ||
const formData = new FormData(); | ||
if (sdkRequest['to'] != null) { | ||
appendArrayToFormData(sdkRequest['to'], 'to', formData); | ||
} | ||
if (sdkRequest['from'] != null) { | ||
formData.append('from', sdkRequest['from']); | ||
} | ||
if (sdkRequest['cc'] != null) { | ||
appendArrayToFormData(sdkRequest['cc'], 'cc', formData); | ||
} | ||
if (sdkRequest['bcc'] != null) { | ||
appendArrayToFormData(sdkRequest['bcc'], 'bcc', formData); | ||
} | ||
if (sdkRequest['subject'] != null) { | ||
formData.append('subject', sdkRequest['subject']); | ||
} | ||
if (sdkRequest['text'] != null) { | ||
formData.append('text', sdkRequest['text']); | ||
} | ||
if (sdkRequest['ampHtml'] != null) { | ||
formData.append('amp-html', sdkRequest['ampHtml']); | ||
} | ||
if (sdkRequest['attachment'] != null) { | ||
appendArrayToFormData(sdkRequest['attachment'], 'attachment', formData); | ||
} | ||
if (sdkRequest['inline'] != null) { | ||
appendArrayToFormData(sdkRequest['inline'], 'inline', formData); | ||
} | ||
if (sdkRequest['recipientVariables'] != null) { | ||
appendSerializedMapToFormData(sdkRequest['recipientVariables'], 'recipient-variables', formData); | ||
} | ||
if (sdkRequest['overrideProperties'] != null) { | ||
appendOverridePropertiesToFormData(sdkRequest['overrideProperties'], formData); | ||
} | ||
if (sdkRequest['customVariables'] != null) { | ||
appendCustomDataToFormData(sdkRequest['customVariables'], 'v:', formData); | ||
} | ||
if (sdkRequest['customHeaders'] != null) { | ||
appendCustomDataToFormData(sdkRequest['customHeaders'], 'h:', formData); | ||
} | ||
if (sdkRequest['html'] != null) { | ||
formData.append('html', sdkRequest['html']); | ||
} | ||
return formData; | ||
}; |
35 changes: 35 additions & 0 deletions
35
...models/v1/emails/request/send-email-html-inline-request/send-email-html-inline-request.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { OverrideProperties } from '../override-properties'; | ||
import { EmailAttachment } from '../email-attachment'; | ||
|
||
export interface SendEmailHtmlInlineRequest { | ||
/** Email address of the recipient(s). Example: `\"Bob <[email protected]>\"`. You can use commas to separate multiple recipients */ | ||
to: string | string[]; | ||
/** Email address for `From` header */ | ||
from: string; | ||
/** Same as `To` but for `Cc` */ | ||
cc?: string | string[]; | ||
/** Same as `To` but for `Bcc` */ | ||
bcc?: string | string[]; | ||
/** Message subject */ | ||
subject: string; | ||
/** Body of the message (text version) */ | ||
text?: string; | ||
/** AMP part of the message. Please follow Google guidelines to compose and send AMP emails */ | ||
ampHtml?: string; | ||
/** File attachment. You can post multiple `attachment` values. **Important:** You must use `multipart/form-data` encoding for sending attachments */ | ||
attachment?: EmailAttachment; | ||
/** Attachment with `inline` disposition. Can be used to send inline images (see example). You can post multiple `inline` values */ | ||
inline?: EmailAttachment; | ||
/** A valid JSON-encoded dictionary, where key is a plain recipient address and value is a dictionary with variables that can be referenced in the message body. See <strong>Batch Sending</strong> for more information */ | ||
recipientVariables?: { | ||
[key: string]: Record<string, string | number | boolean | Date>; | ||
}; | ||
/** @see OverrideProperties */ | ||
overrideProperties?: OverrideProperties; | ||
/** List of */ | ||
customVariables?: Record<string, string | number | boolean | Date>; | ||
/** List of */ | ||
customHeaders?: Record<string, string | number | boolean | Date>; | ||
/** Body of the message (HTML version) */ | ||
html?: string; | ||
} |
1 change: 1 addition & 0 deletions
1
packages/mailgun/src/models/v1/emails/request/yes-no-enum/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type { YesNoEnum } from './yes-no-enum'; |
9 changes: 9 additions & 0 deletions
9
packages/mailgun/src/models/v1/emails/request/yes-no-enum/yes-no-enum.transform.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { YesNoEnum } from './yes-no-enum'; | ||
|
||
// eslint-disable-next-line valid-jsdoc | ||
/** | ||
* ** INTERNAL METHOD ** IT SHOULD NOT BE USED DIRECTLY BY SDK USERS AS IT CAN BE REMOVED OR MODIFIED WITHOUT NOTICE | ||
*/ | ||
export const transformYesNoEnumIntoApiRequestFormat = (sdkValue: YesNoEnum): string => { | ||
return String(sdkValue); | ||
}; |
4 changes: 4 additions & 0 deletions
4
packages/mailgun/src/models/v1/emails/request/yes-no-enum/yes-no-enum.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** | ||
* Toggles opens tracking on a per-message basis, see [Tracking Opens](https://documentation.mailgun.com/docs/mailgun/user-manual/tracking-messages/#tracking-opens). Has higher priority than domain-level setting. | ||
*/ | ||
export type YesNoEnum = 'yes' | 'no' | boolean; |
1 change: 1 addition & 0 deletions
1
packages/mailgun/src/models/v1/emails/request/yes-no-htmlonly-enum/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type { YesNoHtmlonlyEnum } from './yes-no-htmlonly-enum'; |
9 changes: 9 additions & 0 deletions
9
...ilgun/src/models/v1/emails/request/yes-no-htmlonly-enum/yes-no-htmlonly-enum.transform.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { YesNoHtmlonlyEnum } from './yes-no-htmlonly-enum'; | ||
|
||
// eslint-disable-next-line valid-jsdoc | ||
/** | ||
* ** INTERNAL METHOD ** IT SHOULD NOT BE USED DIRECTLY BY SDK USERS AS IT CAN BE REMOVED OR MODIFIED WITHOUT NOTICE | ||
*/ | ||
export const transformYesNoHtmlonlyEnumIntoApiRequestFormat = (sdkValue: YesNoHtmlonlyEnum): string => { | ||
return String(sdkValue); | ||
}; |
4 changes: 4 additions & 0 deletions
4
packages/mailgun/src/models/v1/emails/request/yes-no-htmlonly-enum/yes-no-htmlonly-enum.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** | ||
* Toggles click tracking on a per-message basis, see [Tracking Clicks](https://documentation.mailgun.com/docs/mailgun/user-manual/tracking-messages/#tracking-clicks). Has higher priority than domain-level setting. | ||
*/ | ||
export type YesNoHtmlonlyEnum = 'yes' | 'no' | boolean | 'htmlonly'; |
13 changes: 13 additions & 0 deletions
13
packages/mailgun/src/models/v1/emails/response/email-headers/email-headers.transform.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { EmailHeaders, EmailHeadersFromApi } from './email-headers'; | ||
|
||
// eslint-disable-next-line valid-jsdoc | ||
/** | ||
* ** INTERNAL METHOD ** IT SHOULD NOT BE USED DIRECTLY BY SDK USERS AS IT CAN BE REMOVED OR MODIFIED WITHOUT NOTICE | ||
*/ | ||
export const transformEmailHeadersIntoClientResponse = (apiResponse: EmailHeadersFromApi): EmailHeaders => { | ||
const emailHeaders: EmailHeaders = {}; | ||
for (const [key, value] of apiResponse) { | ||
emailHeaders[key] = value; | ||
} | ||
return emailHeaders; | ||
}; |
5 changes: 5 additions & 0 deletions
5
packages/mailgun/src/models/v1/emails/response/email-headers/email-headers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export type EmailHeadersFromApi = [string, string | Date][]; | ||
|
||
export type EmailHeaders = { | ||
[key: string]: string | Date; | ||
}; |
1 change: 1 addition & 0 deletions
1
packages/mailgun/src/models/v1/emails/response/email-headers/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { EmailHeaders } from './email-headers'; |
Oops, something went wrong.