Skip to content

Instantly share code, notes, and snippets.

@mitchuman
Created July 5, 2023 18:25
Show Gist options
  • Save mitchuman/4c572828b3c4bf983867aa9761212910 to your computer and use it in GitHub Desktop.
Save mitchuman/4c572828b3c4bf983867aa9761212910 to your computer and use it in GitHub Desktop.
/**
* @description Refresh the access token
* @see https://api-console.zoho.com/
* @see https://www.zoho.com/accounts/protocol/oauth/web-apps/access-token.html
* @see https://www.zoho.com/assist/api/refresh-access-token.html
*/
const params = new URLSearchParams()
params.append('refresh_token', process.env.ZOHO_REFRESH_TOKEN as string)
params.append('client_id', process.env.ZOHO_CLIENT_ID as string)
params.append('client_secret', process.env.ZOHO_CLIENT_SECRET as string)
params.append('grant_type', 'refresh_token')
const { access_token } = await fetch(`https://accounts.zoho.com/oauth/v2/token?${ params.toString() }`, {
method: 'POST',
}).then(res => res.json())
/**
* @description Send the email
* @see https://www.zoho.com/crm/developer/docs/api/v4/send-mail.html
* ZohoCRM.send_mail.all.CREATE
*/
const response = await fetch(`https://www.zohoapis.com/crm/v4/Contacts/438903000000012667/actions/send_mail`, {
method: 'POST',
headers: {
'Zoho-oauthtoken': access_token,
},
body: JSON.stringify({
from: {
user_name: proposal.leadOwner,
email: '[email protected]',
},
to: {
user_name: proposal.customerName,
email: proposal.leadId,
},
subject: 'Test Email | GoGreenSolar',
content: `<h1>Test Email | GoGreenSolar</h1><p>This is a test email for proposalId ${proposal.id}</p>`,
mail_format: 'html',
attachments: [
{ id: process.env.ZOHO_T_AND_C_FILE_ID },
]
})
}).then(res => res.json())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment