REST API: send email with attachments (E-MAIL API)

derick3778
Tera Contributor

Hello All,

I would like to send email with attachments using REST API.

Here's what's working:

  1. Adding attachment and getting its sys_id
  2. Sending email associated with specific record.

But how to send e-mail with attachment, Currently I don't see option to specify attachment sys_id in E-MAIL API request.

7 REPLIES 7

DrewW
Mega Sage
Mega Sage

For the system to send an email with an attachment you have to attach the attachment to the sys_email record.  Or you have to store the attachment and then just include a link in the email to the attachment.  I don't believe the Email API supports including an attachment directly.

 

Depending on the other details it may be easier to create a table that will have the email details so you can create a record there and then add the attachment to that record then use a system notification to send the message.

 

 

 

derick3778
Tera Contributor

In which parameter should I attach the attachment link? I already have the attachments with the case, but I want to compose an email and send them with the message via the Email API.

Email API 

Sheldon  Swift
ServiceNow Employee
ServiceNow Employee

Hi @derick3778 - What's the use case? Also, it might be helpful if you share the current configuration.

I have an open case and after completing all operations, another system generates a PDF and I attach it to the case. Therefore, I would like to simulate the "compose email" option with attachment, which is available in the graphical version of the website through the REST Email API. However, there is no option there to send the email with attachment to the client. The idea of ​​what is implemented is below:

BASE_URL = BASE_URL_SN + "api/now/v1/email"    
def send_email(
        self,
        to: list[str] = None,
        subject: str = None,
        text: str = None,
        table_name: str = "sn_case",
        table_record_id: str = None,
        content_type: str = "application/json",
        attachement_file = None, #idea of ​​variable
    ):
        data = {
            "to": to,
            "subject": subject,
            "text": text,
            "table_name": table_name,
            "table_record_id": table_record_id,
        }
        output = requests.post(
            url=self.BASE_URL,
            headers={
                "Authorization": f"Bearer {self.access_token}",
                "Accept": "application/json",
                "Content-Type": content_type,
            },
            files=attachement_file,
            data=json.dumps(data),
        )
        return output

 However, there is no option to send an attachment in this Email API like there is in the "compose email" option on the case page.