You should attach a cover letter to your email unless otherwise specified by an employer. Most employers prefer an attachment because it makes your cover letter easier to save and organize, and is easier to read than scrolling through an email.
Of course, it’s important to pay careful attention to the employer’s job application instructions and to follow these precisely. An employer may request an email cover letter, or they may have a hiring portal where you can attach your cover letter file or copy-paste it into a text box. In such cases, follow the instructions rather than attaching your cover letter to an email.