-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:

abcde@gmail.com
fghijk@gmail.com
lmnop@gmail.com

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
  • 37,005
Dee
  • 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
  • 37,005
  • 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