Yes, stop words can be detected automatically.
Word frequencies in general
One way is to look at the frequency of words in general.
Calculate the frequency of all words in combined texts. Sort them in descending order and remove the top 20% or so.
You can also remove the bottom 5%. These are not stop words, but for a lot of machine learning, they are irrelevant. Perhaps even typos.
Words on the "document"
Another way is to parse the words into a "document".
In a set of documents, stop words can be detected by searching for words that exist in a large number of documents. They would be useless for categorizing or clustering documents in this particular set.
eg. a machine learning system that classifies scientific articles can, after analysis, mark the word "abstract" as a stop word, although it can exist only once per document. But, in all likelihood, almost all of them.
The same can be said of words that can only be found in a very limited number of documents. They are probably mistakenly written or so unique that they will never be seen again.
However, in this case, it is important that the distribution between groups of documents in the training set is even, or that the set divided into one large and one small group can lose all its significant words (since they can exist in too many documents or too few).
Another way to avoid problems with unevenly distributed groups in the training set is to only delete words that exist in all or almost all documents. (For example, our favorite stop words like "a", "it", "the", "an", etc. Will exist in all English texts).
Zipf law
When I studied machine learning and discussed stop words, Zipf's Law was mentioned. However, today I could not tell you how and why, but perhaps this is a general principle or mathematical foundation that you would like to study ...
I googled "Automatic Zipf Law Stop Word Detection", and a quick pick found me two PDF files that might be of interest ...