@bitblit/ratchet-common
Version:
Common tools for general use
49 lines • 2.37 kB
JavaScript
import { RequireRatchet } from '../lang/require-ratchet.js';
import { StringRatchet } from '../lang/string-ratchet.js';
export class MailerUtil {
static EMAIL = new RegExp('.+@.+\\.[a-z]+');
constructor() { }
static convertResolvedReadyToSendEmailToRaw(rts) {
RequireRatchet.notNullOrUndefined(rts, 'RTS must be defined');
RequireRatchet.notNullOrUndefined(rts.destinationAddresses, 'Destination addresses must be defined');
const toLine = 'To: ' + rts.destinationAddresses.join(', ') + '\n';
const bccLine = !!rts.bccAddresses && rts.bccAddresses.length > 0 ? 'Bcc: ' + rts.bccAddresses.join(', ') + '\n' : '';
const from = rts.fromAddress;
const boundary = 'NextPart';
const altBoundary = 'AltPart';
let rawMail = 'From: ' + from + '\n';
rawMail += toLine;
rawMail += bccLine;
rawMail += 'Subject: ' + rts.subject + '\n';
rawMail += 'MIME-Version: 1.0\n';
rawMail += 'Content-Type: multipart/mixed; boundary="' + boundary + '"\n';
rawMail += '\n\n--' + boundary + '\n';
rawMail += 'Content-Type: multipart/alternative; boundary="' + altBoundary + '"\n';
if (StringRatchet.trimToNull(rts.htmlMessage)) {
rawMail += '\n\n--' + altBoundary + '\n';
rawMail += 'Content-Type: text/html; charset="UTF-8"\n\n';
rawMail += rts.htmlMessage;
}
if (StringRatchet.trimToNull(rts.txtMessage)) {
rawMail += '\n\n--' + altBoundary + '\n';
rawMail += 'Content-Type: text/plain\n\n';
rawMail += rts.txtMessage;
}
rawMail += '\n\n--' + altBoundary + '--\n';
if (rts.attachments) {
rts.attachments.forEach((a) => {
rawMail += '\n\n--' + boundary + '\n';
rawMail += 'Content-Type: ' + a.contentType + '; name="' + a.filename + '"\n';
rawMail += 'Content-Transfer-Encoding: base64\n';
rawMail += 'Content-Disposition: attachment\n\n';
rawMail += a.base64Data.replace(/([^\0]{76})/g, '$1\n') + '\n\n';
});
}
rawMail += '\n\n--' + boundary + '--\n';
return rawMail;
}
static validEmail(email) {
return email !== null && MailerUtil.EMAIL.test(email);
}
}
//# sourceMappingURL=mailer-util.js.map