A Malden man has been sentenced to up to four years in prison after pleading guilty to raping a 12-year-old girl he met online.