The correct way to do this is to setup the E-mail server (sendmail, postfix, etc.) to DKIM sign the message, not the routine that sends the message. But yes, it could also be done the way you wish. I assume you are aware that the DKIM public key has to be published via DNS whatever way you go.
As I am very busy now and the next few days. If you don't manage to solve this and you are not in a hurry to do so let me know and I will help.
Best regards,
Tomislav Crnicki
Croatia/Europe