The Temples don't donate food to anyone.They donate food to those who come asking for it and obviously these people are in need of it too and understandably poor.
I will show, in what follows, that by virtue of being poor and in need of food ,they are qualified as worthy recipients of Dana.
If you have but a morsel of food, why don't you give half of it to the
poor ? Will any body ever get his wished-for riches in this life ?
(23)
Vyasa Smriti,Chapter 4,Verse 29
By an intelligent person, seeking his own well-being, these gifts and
others should be made specially unto the poor, the blind and other
distressed persons. (91)
Samvarta Smriti,Chapter 1,Verse 91
O Vasava, O foremoost of the Celestials, eternal is the gift that is
made unto a person who is well-read in the Vedas, born in a good
family, poor, contented, humble, given to the well-being of all
creatures, who studies the Vedas, performs penances..
Vrihaspati Smriti,Chapter 1
So,it is clear that, one of the qualities that make the recipient of Dana worthy, is being poor.
Also,the poor and needy are included among the Poshyas.The society has the responsibility of taking care of their needs,taking care of them.So,the Temples are doing Punya karma only by feeding them.
The father, the mother, the preceptor, the wife, the children, the
poor people, the dependents, the incomers and the guests, are spoken
of as the Poshyas (i.e., those who should be supported . (29)
Daksha Smriti,Chapter 2,Verse 29
Some further relevant verses are given below:
What is presented as a gift to the father, to the mother, to the
.preceptor, to a friend, to a humble person, to one who has done any
good, to the poor, to the helpless and to distinguished persons,
yields fruits. (15)
Daksha Smriti, Chapter 3,Verse 15
Sandhya adorations, bathing, recitation of the Homa, Vedic study,
adorations of the Deities, adoration of the Vis'wadevas, hospitable
treatment extended to the guests, according to one's own might, proper
allotment of food for the departed Manes, Deities, human beings, the
poor, the helpless, the ascetics, the father, the mother and the
preceptor, these are the nine [sacred] works.
Daksha Smriti,Chapter 3
One desiring for lordly powers, should make gifts unto the poor, the
helpless and the learned. By making gifts unto unworthy persons,
people are born dependant on another's fortune.
Daksha Smriti
So,as you can see, by simply being poor and in need of food they should be considered as worthy recipient of Dana as opposed to an unworthy one.
Also,one of the rules for Anna Dana is that it should not be made unasked for.So,here too the Temple authorities are not in fault.They are donating food to only those who are asking for it.
Ayaachtaani deyaani sarva daanaani yatnatah, Annam Vidyaaha
Kanyaachahyanarthibhyona deeyate| Dvaamimau purushaa loke
swargasyopari tishthatah, Anna pradaataa durbhikshe subhikshe hema
vastradah ||
Shandalya Muni mentions that all types of charities be offered without
being asked for and with clean conscience or with no view of securing
returns; however Vidya, Kanya and Anna must not be offered without
being asked for as a rule! Indeed, there are two types of charity
givers who attain Swarga for sure: those who heartily give away Food
during Durbhiksha and also those who happily give away gold and
clothing during the days of general prosperity
With the last quote being taken from the book "Essence of Dharma Bindu" .
And ,moreover,the Temples distribute the Prasadam and not just any food.So,the act is even more sacred and beneficial for everyone involved.