We derive general analytical expressions for the aerodynamic force and moment on a flapping flexible foil undergoing a prescribed undulatory motion in a two-dimensional, incompressible and linearized potential flow from the vortical impulse theory. We consider a fairly broad class of foil motion, characterized by nine non-dimensional parameters in addition to the reduced frequency. Quite simple analytical expressions are obtained in the particular case when just a chordwise flexure mode is superimposed to a pitching or heaving motion of the foil, for which the optimal conditions generating a maximum thrust force and a maximum propulsion efficiency are mapped in terms of the reduced frequency and the relative amplitude and phase shift of the deflection of the foil. These results are discussed in relation to the optimal conditions for a pitching or heaving rigid foil. The present theoretical results are compared with available numerical data for some particular undulatory motions of the flexible foil, with good agreement for small amplitudes of the oscillations and sufficiently high Reynolds number.