Stress and other high arousal states can lead to physical exhaustion and weaken the immune system. Stress and depression are also linked to increased likelyhood of being sedentary, overweight, and suffering from malnutrition.
Other mental illnesses can have direct effects on immune responses, and of course certain medications can have an impact too.
I'm not going to go all "just be happy forehead" here, since I suffer from depression and a few other issues, but it is true that keeping a positive outlook and reducing stress improves your health. Where states of stress, depression, and isolation weaken it.