How would I add error handling to this sp.utility.sendEmail?

Brass Contributor

This function sends an email using the sp.utility.sendEmail from pnp.sp:

public email1 = () => {

  const emailProps: IEmailProperties = {
      To: [this.props.pnltoemail1,this.props.pnltoemail2,this.props.pnltoemail3],
      CC: [],
      BCC: [],
      Subject: "This email is about...",
      Body: "Here is the body. <b>It supports html</b>",
      AdditionalHeaders: {
          "content-type": "text/html"
      }
    };
          
sp.utility.sendEmail(emailProps).then((result) => {
      console.log(result);
   
      console.log("Email Sent!");
   });
  }

 

I had one instance where it didn't send the email. I need some kind of feedback to say whether it sent or not. I've read the documentation: https://pnp.github.io/pnpjs/sp/sp-utilities-utility/

As you can see I'm trying to log the result. I get an odata.null: true but I'm unsure whether that indicates a successfull send. The emails are successfully sending.

Additional: Would I use an error boundary here? After researching, that seems to be overkill. If I receive a 400 return, then how would I capture that and display an error?

1 Reply

@Cardinal_Night  did you find the solution for your issue. I am also trying to send email using @pnp/sp

but I am getting error:

"StartObject' node was found when reading from the JSON reader. A 'StartArray' node was expected."

Please share the answer if you solved it.