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

%3CLINGO-SUB%20id%3D%22lingo-sub-1828465%22%20slang%3D%22en-US%22%3EHow%20would%20I%20add%20error%20handling%20to%20this%20sp.utility.sendEmail%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1828465%22%20slang%3D%22en-US%22%3E%3CP%3EThis%20function%20sends%20an%20email%20using%20the%20sp.utility.sendEmail%20from%20pnp.sp%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-javascript%22%3E%3CCODE%3Epublic%20email1%20%3D%20()%20%3D%26gt%3B%20%7B%0A%0A%20%20const%20emailProps%3A%20IEmailProperties%20%3D%20%7B%0A%20%20%20%20%20%20To%3A%20%5Bthis.props.pnltoemail1%2Cthis.props.pnltoemail2%2Cthis.props.pnltoemail3%5D%2C%0A%20%20%20%20%20%20CC%3A%20%5B%5D%2C%0A%20%20%20%20%20%20BCC%3A%20%5B%5D%2C%0A%20%20%20%20%20%20Subject%3A%20%22This%20email%20is%20about...%22%2C%0A%20%20%20%20%20%20Body%3A%20%22Here%20is%20the%20body.%20%3CB%3EIt%20supports%20html%3C%2FB%3E%22%2C%0A%20%20%20%20%20%20AdditionalHeaders%3A%20%7B%0A%20%20%20%20%20%20%20%20%20%20%22content-type%22%3A%20%22text%2Fhtml%22%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%3B%0A%20%20%20%20%20%20%20%20%20%20%0Asp.utility.sendEmail(emailProps).then((result)%20%3D%26gt%3B%20%7B%0A%20%20%20%20%20%20console.log(result)%3B%0A%20%20%20%0A%20%20%20%20%20%20console.log(%22Email%20Sent!%22)%3B%0A%20%20%20%7D)%3B%0A%20%20%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20had%20one%20instance%20where%20it%20didn't%20send%20the%20email.%20I%20need%20some%20kind%20of%20feedback%20to%20say%20whether%20it%20sent%20or%20not.%20I've%20read%20the%20documentation%3A%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fpnp.github.io%2Fpnpjs%2Fsp%2Fsp-utilities-utility%2F%22%20rel%3D%22nofollow%20noreferrer%20noopener%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Fpnp.github.io%2Fpnpjs%2Fsp%2Fsp-utilities-utility%2F%3C%2FA%3E%3C%2FP%3E%3CP%3EAs%20you%20can%20see%20I'm%20trying%20to%20log%20the%20result.%20I%20get%20an%20odata.null%3A%20true%20but%20I'm%20unsure%20whether%20that%20indicates%20a%20successfull%20send.%20The%20emails%20are%20successfully%20sending.%3C%2FP%3E%3CP%3EAdditional%3A%20Would%20I%20use%20an%20error%20boundary%20here%3F%20After%20researching%2C%20that%20seems%20to%20be%20overkill.%20If%20I%20receive%20a%20400%20return%2C%20then%20how%20would%20I%20capture%20that%20and%20display%20an%20error%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1828465%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
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?

0 Replies