-1

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:

[email protected]
[email protected]
[email protected]

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

jesse_b
  • 35,934
  • 12
  • 91
  • 140
Dee
  • 1
  • 1
  • 1
  • 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 06:21
  • Thanks jesse_b. Your solution helps me too. I have one more scenario. Can you help me . i too have the same emailAdd.txt and i have a list of attachments. they are abc.pdf, def.pdf, ijk.pdf. i want first email id to recieve abc.pdf, then second email id to receive def.pdf and so on. Could you suggest the code? – Emmanuel Fernando May 29 '21 at 18:24

2 Answers2

2
file=/path/to/emailAdd.txt

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.

jesse_b
  • 35,934
  • 12
  • 91
  • 140
  • 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 01:32
0

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:

https://tecadmin.net/ways-to-send-email-from-linux-command-line/

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

#/bin/bash
file="/path/to/emailAdd.txt"

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

email.txt could be setup as follows:

Subject: Hello World

Email Content Hello World
am401
  • 146
  • 6
  • Thank you for that Jesse. I've edited it to correct this problem! – am401 Jul 25 '19 at 14:51
  • You did not fix the problem but you have added some new ones. – jesse_b Jul 25 '19 at 14:55
  • I have tested the for loop with an echo command as I don't currently have a mail client setup, it reads each line and echos each line (I used echo as a test) – am401 Jul 25 '19 at 15:01
  • See [Why does my shell script choke on whitespace or other special characters?](https://unix.stackexchange.com/q/131766/237982) and [Security implications of forgetting to quote a variable in bash/POSIX shells](https://unix.stackexchange.com/q/171346/237982) – jesse_b Jul 25 '19 at 15:04
  • Also [bash pitfalls](http://mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29) – jesse_b Jul 25 '19 at 15:07
  • Thank you for the links, I will have a look at them in due course! – am401 Jul 25 '19 at 15:47
  • tl:dr That is not how you read a file with the shell, see my answer for a more accepted way of doing it. – jesse_b Jul 25 '19 at 15:49