Associating an Email with a Record

Associating an Email with a Record

Does anyone have deluge code they can share on linking a sent email to a custom module? I followed the API documentation but kept getting an error. I could share my code and the error, but I've tried so many different versions already, that I thought I'd just start with whether this already existed somewhere. I've looked and had no luck.

In one scenario we are sending an email through a deluge function from a custom module and upon success we want it to link to the contact (which I thought would be automatic, but it's not).

In the second scenario, we are sending an email through a deluge function from the Contact and upon success we want it to link to the Deal.

Thanks so much!