Skip to content

Commit

Permalink
Recover lost files
Browse files Browse the repository at this point in the history
  • Loading branch information
asein-sinch committed Jan 20, 2025
1 parent 6cf29da commit d4282aa
Show file tree
Hide file tree
Showing 30 changed files with 424 additions and 21 deletions.
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 examples/simple-examples/src/mailgun/emails/sendEmailTemplate.ts
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);

})();
8 changes: 8 additions & 0 deletions packages/mailgun/src/models/v1/emails/request/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,11 @@ export const appendSerializedMapToFormData = (
});
formData.append(key, serializedData);
};

export const transformDateIntoApiRequestFormat = (date: Date | string): string => {
if (date instanceof Date) {
return date.toUTCString();
} else {
return date;
}
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { OverrideProperties } from './override-properties';
import { appendDeliveryTimeOptimizePeriodToFormData } from '../helpers';
import {
appendArrayToFormData,
appendDeliveryTimeOptimizePeriodToFormData,
transformDateIntoApiRequestFormat,
} from '../helpers';
import { transformYesNoHtmlonlyEnumIntoApiRequestFormat } from '../yes-no-htmlonly-enum/yes-no-htmlonly-enum.transform';
import { transformYesNoEnumIntoApiRequestFormat } from '../yes-no-enum/yes-no-enum.transform';
import { appendArrayToFormData } from '../helpers';
import FormData = require('form-data');

// eslint-disable-next-line valid-jsdoc
Expand All @@ -26,7 +29,7 @@ export const appendOverridePropertiesToFormData = (sdkRequest: OverridePropertie
formData.append('o:secondary-dkim-public', sdkRequest['secondaryDkimPublic']);
}
if (sdkRequest['deliveryTime'] != null) {
formData.append('o:deliverytime', sdkRequest['deliveryTime']);
formData.append('o:deliverytime', transformDateIntoApiRequestFormat(sdkRequest['deliveryTime']));
}
if (sdkRequest['timeZoneLocalize'] != null) {
formData.append('o:time-zone-localize', sdkRequest['timeZoneLocalize']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface OverrideProperties {
/** Specify an alias of the domain key specified in `o:secondary-dkim`. Also formatted as `public_signing_domain/selector`. `o:secondary-dkim` option must also be provided. Mailgun will sign the message with the provided key of the secondary DKIM, but use the public secondary DKIM name and selector. Note: We will perform a DNS check prior to signing the message to ensure the public keys matches the secondary DKIM. */
secondaryDkimPublic?: string;
/** Specifies the scheduled delivery time in RFC-2822 format (https://documentation.mailgun.com/docs/mailgun/user-manual/get-started/#date-format). Depending on your plan, you can schedule messages up to 3 or 7 days in advance. If your domain has a custom message_ttl (time-to-live) setting, this value determines the maximum scheduling duration. */
deliveryTime?: Date;
deliveryTime?: Date | string;
/** Toggles Timezone Optimization (TZO) on a per message basis. String should be set to preferred delivery time in `HH:mm` or `hh:mmaa` format, where `HH:mm` is used for 24 hour format without AM/PM and hh:mmaa is used for 12 hour format with AM/PM. See **Sending a Message with TZO** for details. *Please note that TZO is only available on certain plans. See www.mailgun.com/pricing for more info* */
timeZoneLocalize?: string;
/** 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. */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type { SendEmailHtmlInTemplateRequest } from './send-email-html-in-template-request';
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;
};
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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type { SendEmailHtmlInlineRequest } from './send-email-html-inline-request';
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;
};
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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type { YesNoEnum } from './yes-no-enum';
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);
};
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;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type { YesNoHtmlonlyEnum } from './yes-no-htmlonly-enum';
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);
};
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';
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;
};
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;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { EmailHeaders } from './email-headers';
Loading

0 comments on commit d4282aa

Please sign in to comment.