Im a newbie for scripting. Could you guys help me on how to read email address from a text file and send a email to those email address.

For example I have a emailAdd.txt

Inside that txt file:


And I want to send “Hello World” to those email. This is just an example, I need to send to around 30 email address.

  • Thanks jesse_b. Your way is to send email one by one right? May I know how to send to many, not one by one. – Dee Jul 26 '19 at 6:21

while read -r email; do
    printf '%s\n' 'Hello, world!' | mail -s 'This is the email subject' "$email"
done < "$file"

This will loop through each line in the txt file and set the email variable to the full line (which is only an email address in your example data). It will the print Hello, world! and send that to each email with the subject This is the email subject.

See mail(1)

Note emails sent from mail/mailx/sendmail will often be caught by your emails spam filter.

  • I think mail should add the appropriate headers by itself? Though you may want to override the From address if your system doesn't receive mail (-r in the two different mail utils I checked). And if you do create a file with the headers included, I don't think you can send it just like that with mail, the "headers" will just become part of the message – ilkkachu Jul 25 '19 at 14:52
  • @ilkkachu: It works for me that way although to be fair I use mailx. I just cat file | mailx – jesse_b Jul 25 '19 at 14:55
  • well yeah, I think the important question is "which mailx". (I think e.g. Debian has at least three packages that provide an alternative /usr/bin/mail) The one I've used doesn't pick headers from the message proper. – ilkkachu Jul 25 '19 at 14:56
  • @ilkkachu: mailx version 5.0 and yea sorry I also use the -t option which "scans input for to:, from:, and bcc: fields". It also works fine to pull the subject line though too so I think it is reading the header. Although that is not the same version of mail/mailx on my mac. – jesse_b Jul 25 '19 at 14:58
  • Your way is to send email one by one right? May I know how to send to many, not one by one. – Dee Jul 29 '19 at 1:32

It depends on what email client you have on the CLI. As far as looping through the emailAdd.txt you can accomplish this with a for loop. You can find how to send CLI emails using five different ways on this website:


However here is an example that will do as you've asked:


while read -r line
    sendmail $email  < /path/to/email.txt
done < "$file"

email.txt could be setup as follows:

Subject: Hello World

Email Content Hello World

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.