In the Email Queue, there is a “Send Now” or “Retry Sending” button

In the Email Queue , there is a “Send Now” or “Retry Sending” button. I want to trigger this button using a scheduler when the email queue status is “Error” or “Not Sent”

Failed emails auto-retried based on the Email Retry Limit.

My problem is that when a Sales Invoice is created using the scheduler, the email is not sent afterward.
How can I achieve this?