add nested if to prevent page breaking on email error