Identifying galaxy clusters through overdensities of galaxies in photometric surveys is the oldest1,2 and arguably the most economical and mass-sensitive detection method3,4, compared with X-ray5-7 and Sunyaev-Zel'dovich effect8 surveys that detect the hot intracluster medium. However, a perennial problem has been the mapping of optical `richness' measurements onto total cluster mass3,9-12. Emitted at a conformal distance of 14 gigaparsecs, the cosmic microwave background acts as a backlight to all intervening mass in the Universe, and therefore has been gravitationally lensed13-15. Experiments such as the Atacama Cosmology Telescope16, South Pole Telescope17-19 and the Planck20 satellite have now detected gravitational lensing of the cosmic microwave background and produced large-area maps of the foreground deflecting structures. Here we present a calibration of cluster optical richness at the 10% level by measuring the average cosmic microwave background lensing measured by Planck towards the positions of large numbers of optically selected clusters, detecting the deflection of photons by structures of total mass of order 1014 M⊙. Although mainly aimed at the study of larger-scale structures, the Planck estimate of the cosmic microwave background lensing field can be used to recover a nearly unbiased lensing signal for stacked clusters on arcminute scales15,21. This approach offers a clean measure of total cluster masses over most of cosmic history, largely independent of baryon physics.