As you rotate the dipole through an infinitesimal angle \(d\theta\), you do an amount of work
$$dW=(\overrightarrow{p}\cdot\overrightarrow{E})sin\theta d\theta=pEsin\theta d\theta$$
In a finite rotation from angle \(\theta_1\) to angle \(\theta_2\), the work done is:
$$W=\int_{\theta_1}^{\theta_2}dW=pE\int_{\theta_1}^{\theta_2}sin\theta d\theta=pE(cos\theta_1+cos\theta_2)$$
In the above equation \(\theta_1\) is the initial angle and \(\theta_2\) is the final angle of the dipole with respect to the field direction.
To get \(W\) in terms of initial orientation only, we substitute \(\theta_2=\pi-\theta_1\) into the above equation.Therefore
$$W=-2pEcos\theta_1$$
$$W\propto cos\theta_1$$
This equation implies that the work is maximum when the dipole is initially antiparallel to the field and zero if it is initially parallel.