0

I am using Cloudflare worker to accept a post request and mail it then using Spark post Api. I am getting content.text must be a string error when I send it to Api.

async function sendEmail(request, SPARKPOST_API_KEY) {
    const jsonData = await request.json();
    const city = jsonData.city;
    const state = jsonData.state;
    const message = city + "," + state;

    const body = JSON.stringify({
      options: {
        sandbox: false,
      },
      content: {
        from: FROM_EMAIL,
        subject: 'Data Submission',
        text: message
      },
      recipients: [{ address: TO_EMAIL }],
    });

If I do the following the mail gets send without any issue.

content: {
        from: FROM_EMAIL,
        subject: 'Data Submission',
        text: "Hello World"
      },

Also, I tried doing the following.

const message = "<p>${city}<p>";
const body = JSON.stringify({
      options: {
        sandbox: false,
      },
      content: {
        from: FROM_EMAIL,
        subject: 'Data Submission',
        html: message
      },
      recipients: [{ address: TO_EMAIL }],
 });

But then I would get content.html must be a string. Also is there any way I can send city and state so they each appear on a new line.

amren
  • 3
  • 3
  • Maybe try logging the JSON to see exactly what `content.text` looks like? I agree from your code it sure looks like it should be a string, but this will tell you for sure. – Kenton Varda Aug 07 '23 at 14:20

1 Answers1

0

For anyone facing a similar issue in the future, formatting like this worked for me.

body = JSON.stringify({
      options: {
        sandbox: false,
      },
      content: {
        from: FROM_EMAIL,
        subject: "New Student Application",
        html: `<div><p>Student Name: ${jsonData.studentName}</p><p>Gender: ${jsonData.gender}</p><p>Contact No: ${jsonData.contactNumber}</p></div>`,
      },
      recipients: [{ address: TO_EMAIL }],
    });
amren
  • 3
  • 3
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 28 '23 at 10:27