Some types of pneumonia such as the Fungal pneumonia can pass from the environment to a person, but Fungal pneumonia is not contagious from person to person.
Other types of pneumonia that are caused by viruses or bacterial infections are contagious and can be spread from person to person so you should wear a mask when around a person who has the type of pneumonia caused by bacteria or viruses.