Flow of CO2 in wells is associated with substantial variations in thermophysical properties downhole, due to the coupled transient processes involved: complex flow patterns, density changes, phase transitions, and heat transfer to and from surroundings. Large temperature variations can lead to thermal stresses and subsequent loss of well integrity, and it is therefore crucial to employ models that can predict this accurately. In this work, we present a model for vertical well flow that includes both two-phase flow and heat conduction. The flow is described by a two-fluid model, where mass transfer between the phases is modelled by relaxation source terms that drive the phases towards thermodynamic equilibrium. We suggest a new formulation of the mass transfer process that satisfies the second law of thermodynamics, and that is also continuous in the single-phase limit. This provides a more robust transition from two-phase to single-phase flow than the previous formulation. The model predicts which flow regimes are present downhole, and calculates friction and heat transfer depending on this. Moreover, the flow model is coupled with a heat conduction model for the layers that comprise the well, including tubing, packer fluid, casing, cement or drilling mud, and rock formation. This enables prediction of the temperature in the well fluid and in each layer of the well. The model is applied to sudden shut-in and blowout cases of a CO2 injection well, where we employ the highly accurate Span-Wagner reference equation-of-state to describe the thermodynamics of CO2. We predict pressure, temperature and flow regimes during these cases and discuss implications for well integrity. © 2016 Elsevier Ltd.