Send a mail and relate it to a contact in logic_hook

Hello all
I have an “after_save” hook process that sends a mail to a contact using PHP code and PHPMail internally.

I would like this mail that the system sends automatically in my custom code to be related as a mail sent to the contact in the “History” of activities of the contact.

Is there any way I can create an “Email” record (with all the fields and addting attachment) and relate it to the contact itself so then once the users go to the History of Activities of the contact, this mail appers in it?

Any example I can check please?

Thanks so much!