Heating and cooling of magnetars with accreted envelopes