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.
However, read the job ad carefully and follow the instructions you’re given. If the job ad specifically states that you should write an email cover letter, don’t attach your letter instead. Employers may also have a hiring portal where you can attach your cover letter file or copy-paste it into a text box.