mirror of
https://github.com/ToolJet/ToolJet
synced 2026-04-24 06:57:40 +00:00
* Added option to send Sender's Name * change made to Sender Name * bump version to 3.20.46-lts in all relevant files --------- Co-authored-by: Pratush <pratush@Pratushs-MacBook-Pro.local> Co-authored-by: gsmithun4 <gsmithun4@gmail.com>
43 lines
1.4 KiB
TypeScript
43 lines
1.4 KiB
TypeScript
import { QueryError, QueryResult, QueryService } from '@tooljet-plugins/common';
|
|
import sgMail from '@sendgrid/mail';
|
|
import { EmailOptions, SourceOptions, QueryOptions } from './types';
|
|
|
|
export default class SendGridQueryService implements QueryService {
|
|
async run(sourceOptions: SourceOptions, queryOptions: QueryOptions, dataSourceId: string): Promise<QueryResult> {
|
|
if (!(queryOptions && sourceOptions.api_key)) {
|
|
throw new QueryError('Query could not be completed as API key is not set', 'Missing API key', {});
|
|
}
|
|
|
|
sgMail.setApiKey(sourceOptions.api_key);
|
|
|
|
let result = {};
|
|
|
|
const fromAddress = queryOptions.send_mail_from_name && queryOptions.send_mail_from_name.trim()
|
|
? { email: queryOptions.send_mail_from, name: queryOptions.send_mail_from_name.trim() }
|
|
: queryOptions.send_mail_from;
|
|
|
|
const sendgridEmailOptions: EmailOptions = {
|
|
to: queryOptions.send_mail_to,
|
|
from: fromAddress,
|
|
subject: queryOptions.subject,
|
|
text: queryOptions.text,
|
|
isMultiple: queryOptions.multiple_recipients ?? false,
|
|
};
|
|
|
|
if (queryOptions.html && queryOptions.html.length > 0) {
|
|
sendgridEmailOptions.html = queryOptions.html;
|
|
}
|
|
|
|
try {
|
|
result = await sgMail.send(sendgridEmailOptions);
|
|
} catch (error) {
|
|
console.log(error.response);
|
|
throw new QueryError('Query could not be completed', error.message, {});
|
|
}
|
|
|
|
return {
|
|
status: 'ok',
|
|
data: result,
|
|
};
|
|
}
|
|
}
|