A retirement letter has two jobs at once. It is a formal notice that creates a record of your last working day, and it is very often the last written word you leave with an employer you may have spent decades alongside. That combination is why it looks like a resignation letter in structure but reads very differently in tone.
Open by stating plainly that you are retiring, your job title, and the exact date. Name your years of service, since that context is the reason the rest of the letter exists. Then close with genuine thanks, sized to how long you were actually there: a short, warm line after two years, more room to reflect after twenty.