I'm sorry to hear about your dog.
Head shaking and foaming at the mouth are not a good sign - these symptoms could be indicative of a seizure or poisoning, among other things.
One thing that comes to mind is epilepsy or a seizure. This can cause the dog to foam at the mouth, shake his head, have tremors, periods of time where the dog appears "spaced," chewing at the air, loss of bowels or bladder control, unusual eye movements, and an array of other symptoms. This really requires a vet's attention as soon as possible, as seizures can ber dangerous in and of themselves.
Unfortunately, seizures can also be indicative of other serious problems, such as infection or poisoning, all of which can be deadly, so it's important to get your dog to a vet as soon as possible. You may want to go through your home and yard to see if there's signs that your dog could have gotten into anything, such as a household chemical, food, toxic houseplant, etc. Outside, if your dog has had access in recent hours, you'll want to look for the same - toxic plants, chemicals or signs that your dog has ingested an animal, such as a rodent or other animal. An animal who has been poisoned can cross the path of a dog or other predator, and since they're ill, they're easier to catch, and when the dog ingests the animal, the poison can be transferred to their system.
These website links will take you to a site that lists some of the other symptoms of seizure and the different types of seizure:
I'm also going to include some links to sites that list toxic plants food, and household items:
In the case of poisoning, you may also want to contact an animal poison control center. There can be a fee in some cases, and note that one number is XXXXX 900-number. These are good numbers to have (two different agencies), in any event: 1-888-426-4435 and 1-900-680-0000.
I wish I had better news for you. I hope your dog makes a full recovery and that he's feeling better soon. Let me know if there's anything else I can do to assist you.
****Please "ACCEPT" if my answer was helpful!****