The Talmud says that a person should pray only out of the joy associated with performing a mitzva.
Prayer, which is fundamentally an emotional service, clearly requires an appropriate emotional state.
Our Sages broaden the scope of this requirement to include other mitzvot, such as Torah study, in another talmudic dictum which states that the Divine Presence rests only on a person who is imbued with the joy associated with a mitzva.
When a person is feeling dejected or melancholy, it is as though he is trying to function in the dark.
He cannot fully utilize his abilities.
In order to illuminate the darkness and “purify and illuminate his soul,” he must experience joy.
Then he can look at himself and at the world around him with joy, energy, and ease.
Rabbi Adin Steinsaltz