The anal glands or anal sacs are small glands near the anus in many mammals , including dogs  and cats. They are paired sacs on either side of the anus between the external and internal sphincter muscles. Sebaceous glands within the lining secrete a liquid that is used for identification of members within a species. These sacs are found in many carnivorans , including wolves ,  bears ,   sea otters  and kinkajous. The anal glands are in the wall of the anal canal.
You See Bleeding or an Abscess on Your Dog’s Bottom… What Does It Mean?
Everything You Ever Wanted to Know About Dog Anal Glands
Dog Breed Guide. Cat Breed Guide. Pet Health Conditions. Weekly pet tips - sent straight to your inbox.
Pet Health Hub
It is believed that this smelly substance helps a dog mark their territory. If an anal gland becomes blocked, an infection or abscess may develop. This can be extremely painful and needs treatment by a vet. They should empty themselves when your dog goes to the toilet. Anal gland infections are nearly always a result of leaving blocked anal glands without treatment.
They are pockets which vary in size depending on the size of dog and presence of any disease, and have small openings towards the opening of the anus. These sacs tend to empty their contents when a dog passes faeces. On occasion they can spontaneously empty when a dog barks or jumps, or can even do so without any sudden activity, expelling their liquid and scent to the outside.