Add email attachments to contact

Automatically add email attachments to the Attachments tab of a contact record whenever you receive an email from that contact in your CRM.

Businesses often receive essential documents from their customers to complete the sales and onboarding process. These may include business registration certificates, RFP documents, product specifications, non-disclosure agreements, and other similar items. Since these documents reside inside various emails, it can be easy to lose track of them.

It is a best practice to add files sent over email to the Attachments tab of the corresponding contact record. This ensures that all relevant documents are accessible in one location without sifting through individual emails. However, manually downloading and re-uploading the email attachments into the contact can be time consuming. Is there a way to do it automatically every time you receive an email? Fortunately, our low-code tools can make it possible.

Using functions, you can automatically add email attachments to the Attachments tab of a contact record every time you receive an email from that contact. All you need to do is create a custom function and connect it to a workflow rule for the Email module. Read on to learn more.

Permissions and availability

  • Users with the Manage Extensibility permission can create connections and write custom functions.
  • Users with the Manage Automation permission can create and update workflow rules.
  • Users with the Manage Sandbox permission can manage the sandbox.

Requirements

  • Create a Zoho OAuth connection with the required scopes for your Zoho CRM account, as detailed in the "Create Connection" section below. Learn more
  • Create a workflow rule for the Emails module that triggers every time an incoming email is received for a contact in your CRM. Learn more
  • Write a custom Deluge function to add email attachments to the Attachments tab of the contact record and link it to the workflow rule. Learn more
  • Test the workflow rule in a sandbox before deploying it in your production environment. Learn more

Create a connection

The first step is to create an OAuth connection in your Zoho CRM account.

  1. Navigate to Setup > Developer Space > Connections.
  2. Click Create Connection.
  3. Select Zoho OAuth under Default Services.
  4. Specify a Connection Name.
    You must enter this name in the code snippet below.
  5. Select the following Scopes to request access.
    • ZohoCRM.modules.ALL
    • ZohoCRM.send_mail.all.CREATE
    • ZohoCRM.settings.ALL
    • ZohoCRM.settings.emails.READ
  6. Click Create and Connect.
  7. Click Connect, then click Accept to confirm authorization of access for the scopes requested by the client.

Create a workflow rule

The next step is to create a workflow rule linked to a custom function.

  1. Navigate to Setup > Workflow Rules > +Create Rule.
  2. Select the Module as Emails from the drop-down list.
  3. Provide a name for the rule. For example: "Add contacts' email attachments to their Attachments tab".
  4. Add a description, if required, then click Next.
  5. In the Execute this workflow rule based on section, do the following:
    1. Choose Incoming Email, then select Received from the drop-down.
    2. Click Next.
  6. In the Which emails would you like to apply the rule to? section, do the following:
    1. Set conditions for email fields if necessary.
    2. In Apply this rule to, choose Contact.
    3. Choose if you want to apply the rule to all contacts or only those matching certain conditions.
    4. Click Next.
  7. Under Instant Actions, select Function and Write your own.
  8. Provide a name for the function, as well as a description, if necessary.
  9. On the Deluge Script Editor, do the following:
    1. Copy and paste the code given below.
    2. Click Edit Arguments.
    3. Choose Contacts - Contact Id and name it id.
    4. Click the + icon to add more arguments.
    5. Choose Contacts - Contact Owner ID, name it userid, then click Save.
    6. Click Save & Execute script.
  10. Click Save on the workflow rule page.

The code

Code Copied
curl = "https://www.zohoapis.com/crm/v6/Leads/" + id + "/Emails";
resp = invokeurl
[
	url :curl
	type :GET
	connection:"connectionname"
];
messageid = resp.get("Emails").get(0).get("message_id");
curl = "https://www.zohoapis.com/crm/v6/Leads/" + id + "/Emails/" + messageid;
re = invokeurl
[
	url :curl
	type :GET
	connection:"connectionname"
];
atta = re.get("Emails").getJson("attachments");
if(atta != null)
{
	info "Attachment : " + atta;
	for each  ele in atta
	{
		aid = ele.get("id");
		filename = ele.get("name");
		curl = "https://www.zohoapis.com/crm/v6/Leads/" + id + "/Emails/actions/download_attachments?message_id=" + messageid + "&user_id=" + userid + "&id=" + aid + "&name=" + filename;
		re = invokeurl
		[
			url :curl
			type :GET
			connection:"connectionname"
		];
		info "re : " + re;
		upd = zoho.crm.attachFile("Leads",id,re);
		info "Upload attachment : " + upd;
	}
}

Notes

  • Replace "connectionname" with the name of the connection you specified while creating the connection (see the "Create a connection" section above).
  • Make sure to use the accurate API names for their corresponding fields (e.g., "Contacts") in the code snippet. Learn more
  • The function does not retrieve the inline attachments included in the email.

Tip

  • Configure and test the function in a sandbox to ensure that further development doesn't disrupt your production environment.

Test the solution

  1. Navigate to the Contacts module.
  2. Create a contact with an email address that has a functioning mailbox for sending emails.
  3. Send an email with attachments from the contact's mailbox to your company's email address.
    The contact will indicate the email as received in the Emails tab.
  4. Check whether the email attachments were added underneath the contact's Attachments tab.

Did you find this useful? Try it out and let us know how it works. Share this with your team if they'd benefit from it! If you have questions, please don't hesitate to contact us.

More info

  • ModuleContacts
  • Trigger PointWorkflow Rule
  • EditionEnterprise and above
  • ComplexityHigh
  • Implementation Time60 minutes

Features used in the solution

ConnectionWorkflow RuleDeluge ScriptSandbox
Story Image

Looking for a custom solution?

Contact us, we will help enhance your productivity at lightning speed.

SUBMIT REQUEST

Developers: Share your solution with our community!