How To Use The Commuter Rail?

As a native I feel so stupid asking this, but I need to learn lol! I have lived in Boston my whole life, but have never had to take anything but the regular train, trolley, bus lines, etc. Recently, I tried the commuter rail for the first time and was so confused.

The fare machine at the station I was at was not working, so I got on without paying. Some nice guys at the station, one being an off-duty MBTA worker and another being a random guy (his friend), saw that I was confused and told me that the specific station we were at is just prone to having out-of-order machines, and that it's okay to get on without paying. However, I would like to know how to pay for stations that aren't like that one!

Is there a physical ticket I'd buy at the machine (if it were working)? Can I use my Charlie Card? Can I use my phone to pay with the credit card NFC feature? When do I pay; when I get on, off, or do they come around to check tickets (and I just happened to not see anyone on my singular ride)? Is there anything else I should know?

Thanks!