Medplum Client SendEmail API
# support
b
I noticed when I attempted using the medplum client send email API in our react application that I am getting some CORS errors. Is there additional configuration required in order to get the sendEmail API working?
r
Are you calling it from the client?
b
Yes
Will I need to setup a BE service to avoid CORS?
r
I believe that will not work - I think the intent is to use it server side or in bots
Bots will work
I need to double check whether it can work from the client
b
The bots will be too much overhead work for us for sending out a simple email, are there any alternatives with medplum that will allow us to quickly utilize the email API on client side?
Unless it's easier than I'm thinking it is to write one in medplum
r
in general client side email is rare, but I can double check
b
We can create a medplum email service in our Python FastAPI if that's an option
Documentation seems to imply that we can use sendEmail here
r
Yes, that is supported from Bots
b
Got it, can you point me to documentation I can refer to for setting up the medplum bot? Is this the recommended way of using medplum's email API ?
Thank you for your help !
r
Yes - here's the basics: https://www.medplum.com/docs/bots/bot-basics - and we should update the docs to be more clear
b
I was able to get the bot to run certain actions like
medplum.createPdf
but it's not working for sendEmail. I'm running into this error when trying to use the bot to send an email:
{"errorType":"Error","errorMessage":"Failed to send email: Error: Forbidden","trace":["Error: Failed to send email: Error: Forbidden"," at Object.handler (/var/task/user.js:19:15)"," at process.processTicksAndRejections (node:internal/process/task_queues:95:5)"," at async exports.handler (/var/task/index.js:15:12)"]}
email input is:
Copy code
{
  "to": "name@gmail.com",
  "subject": "Test Subject",
  "text": "Test Email Body"
}
code for bot: `import { BotEvent, MedplumClient, MailOptions } from '@medplum/core'; export async function handler(medplum: MedplumClient, event: BotEvent): Promise { if (typeof event.input !== 'object' || !event.input) { throw new Error('Invalid input data'); } const email = event.input as MailOptions; try { const response = await medplum.sendEmail(email); if (!response) { throw new Error('Failed to send email'); } console.log('Email sent successfully'); return true; } catch (error) { console.error(
Failed to send email: ${error}
); throw new Error(
${email} Failed to send email: ${error}
); } } `
r
Can you DM me your project ID? https://app.medplum.com/admin/project
r
@boogiecoco. - apologies, sending emails from Bots is a paid feature, but it had not been enabled on your account. Could you try your Bot again to check to see if it works?
b
Same issue still
r
Sorry about that. Can you try one more time?
b
API is successful now, but I'm not seeing an email in my inbox.
Here is the response:
Copy code
{
  "resourceType": "OperationOutcome",
  "id": "ok",
  "issue": [
    {
      "severity": "information",
      "code": "informational",
      "details": {
        "text": "All OK"
      }
    }
  ]
}
Here is the payload:
Copy code
{
  "to": "vijaypraju04@gmail.com",
  "subject": "Test Subject",
  "text": "Test Email Body"
}
r
Sorry for the dumb question, but can you check your spam folder?
b
Just got one of them, think there may have just been a long delay
Are there any options for styling the email template?
r
Under the hood we use the
nodemailer
library, so we would support any functionality in that library regarding rendering https://nodemailer.com/message/
b
Got it, so we can use the HTML Field, thank you so much !
r
@boogiecoco. , I also wanted to share some good news. We discussed with our security team, and seems like we should be allowed to enable sending email directly from the client (no CORs).
I'll let you know when it's been deployed
b
Amazing, thanks for letting me know.
One more quick question related to bots, is it a good use case to use bots for assessments like PATs? Asking because I noticed this example: https://www.medplum.com/docs/bots/bot-for-questionnaire-response
r
Yes, bots give a flexible mechanism for handling QuestionnaireResponses (The FHIR version of FormData). In your bot, you can take this data as input and create / update resources, call out to external APIs, etc
b
Does the bot API also have access to the GraphQL API for mutations? Or is it recommended to use the standard REST API
r
The bot receives a MedplumClient, and has full access to all the methods avaiable to the MedplumClient class: https://www.medplum.com/docs/sdk/classes/MedplumClient
That includes graphql 🙂
b
Does using the bot as an auto save mechanism when someone is filling out a long questionnaire make sense as a use case? Actually I’m not sure if that makes sense since we’d have to periodically make requests to the bot API endpoint
r
We don't havea a built-in autosave mechanism right now. But the QuestionnaireResponse field has a
status
field which could be either 'in-progress' or 'completed' https://www.medplum.com/docs/api/fhir/resources/questionnaireresponse
You could set up your Bot to only subscribe to QRs that are "completed", and implement an autosave mechanism for drafts
@boogiecoco. - The client-side email has been merged! Should be deployed in ~1hr https://github.com/medplum/medplum/pull/2455
b
Awesome thank you ! I'm running into some issues with attachments, we're getting our files back as binary strings, but the send email API isn't accepting the binary string, are there any restrictions on this on medplum's side or is it all based on nodemailer ?
Does it require us to create a FHIR Binary ?
Copy code
* See the FHIR "create" operation for full details: https://www.hl7.org/fhir/http.html#create
     *
     * @category Create
     * @param data The binary data to upload.
     * @param filename Optional filename for the binary.
     * @param contentType Content type for the binary.
     * @returns The result of the create operation.
     */
    createBinary(data: string | File | Blob | Uint8Array, filename: string | undefined, contentType: string, onProgress?: (e: ProgressEvent) => void): Promise<Binary>;
we'll improve the docs on this, but creating a FHIR binary is needed here so that the binary file can be stored in Medplum binary storage system
b
Ahhh gotcha and for how we are storing document references, would it make sense to store the created binary in the content fields of document reference ?
r
Yep, you can refer to this doc for an example: https://www.medplum.com/docs/charting/external-documents
b
For some reason the email doesn't work in production for us, is this another issue of needing to enable the bot service?
r
Potentially. Can you DM me your projectid?
We've also removed the CORs error, if you would like to send email directly from the client. Feature was merged into the server yeseterday: https://github.com/medplum/medplum/pull/2455
@boogiecoco. - nvm, I think I found your prod project. Please try again at your convenience
b
works now, thank you !
r
Were you able to try the client-side email sending as well? Just wanted to confirm that it works for you
b
Yes client side works ! Not using the bot
299 Views