First superman, the most important thing you can do for yourself is to realize here and now, you're not by yourself. --Other men experience this kind of thing at different stages of their lives. The difference with you is that you're willing to talk about it and ...get advice, even help. And, that's good.
You've not given us your age and in some ways, that too, it neither here nor there.
(Sometimes men over 30 or so will encounter difficulties getting or staying erect.)
Your difficulty might even be plain ole stress and anxiety--worrying about "performing" in the act of sex. That too, it most common. Men never admit to it, though. I'm almost willing to bet this is your problem perhaps and not one that concerns any physical problem with your genito/urinary system.
Solo sex (mastubation) is quite different than when with another person. That fact alone can account for the ease in staying hard....there is no pressure on your ability to "perform" and satisfy another person. Often performance anxiety occurs, but with an understanding partner and over time --improved erections do return. Embarrassing as this matter is for you just now, it shouldn't be the end of the world. There are other ways to enjoy sex without intercourse per se and perhaps those other types of sexual stimulation approaches could help lessen your current tension and the demand of having to perform! Eventually, your erection quality could even improve on its own.
Your smoking could very well contribute to your circulation system not being up to par---heavy smokers can indeed have erectile problems! Good circulation is imperative to good "male health". Without being scared to pieces, you should feel confident in seeking a thorough medical workup for just easing your mind if you're that worried. You won't be the only man seeing a doctor about not having good erections. And, as you already know, you might want to consider giving up the smoking, not for your penis, but just to improve your overall health and well-being.
Since you asked--yes, Viagra could help you. But, it also could not necessarily even be needed! I dislike how readily Viagra comes to the rescue for men anyway. I never recommend it to any man, at any age, as a "quick fix". It should ideally always be used with medical supervison, too! It's generally very safe, but medical history (circulatory and heart histories!
should be proven sound before altering blood flow in the body with Viagra (which is how this and other erectile related drugs achieve their affects). It's really serious business when one seriously thinks about it!
Sometimes simple, even the most basic nutritional supplements to boost male health can achieve wonders too, for improved erections. Remember that spinach!
(Sorry, superman---I couldn't resist!
)
Anyway, I've given you what info I could to help guide your thinking.