How do I send an email using awk to a list of email addresses in a text file? - linux

If I have a text file with a list of email addresses, how can I go through the list and send an email to each of those email addresses with a text file as the message.
I.e. I want to take in an email as a variable so I can execute this command:
mail -s "Welcome" email#address.com < welcome.txt

for example you have a mails_addresses.txt file with one address per line like that:
email1#mail.com
email2#mail.com
email3#mail.com
In case you have another complex structure which you need to parse with for example awk you should to show it us.
So you need just to write a loop which will read it and send it to mail command:
while read MAIL
do
mail -s "Welcome" "$MAIL" < welcome.txt
done < mails_addresses.txt

You can do this even without awk:
cat users-list | while read addr
do
mail -s "Welcome" "$addr" < welcome.txt
done

Related

linux command - how to send a notification email using shell script to administrator

How can I send in Linux a notification email using shell script to administrator
about monitor a given sites/machines, given at runtime, and notifies an administrator by email if any of the sites/machines becomes unresponsive
send, in Linux, a notification email to administrator (I assume: local root account):
echo "simple ascii only message here" | mail -s "subject line" root
or
cat notification_file | mail -s "subject line" root
#note: notification_file's content will be the "body" of your email. Not an attachment.
#It should contain only regular ASCII characters... ie, basically:
# letters, numbers, punctuations, newlines, tabs, spaces.
#Anything else could be tricky. If you need more (unicode? attachments?),
#you should use a variant (ex: mutt ?) instead of the basic "mail" command
If you meant an outside mail, to a work/personnal email, you'll need to read a bit about how mail systems work, as by default it will not be able to reach the correct smtp server in the correct way...

mail can't send messages: Process exited with a non-zero status

I wrote a bash script that sends out a mail, but after 50 e-mails it starts to say "mail can't send messages: Process exited with a non-zero status". Can anyone help solve my problem. The code I used is below if you want to take a look at it.
#!/bin/bash
#Declare variables area.
emailBody=email_body.txt; #you have to use without “ symbol for some reason
emailList=email_list_delimiter.txt;
#send mail command. using a read file loop.
while IFS= read -r emailTo; do
cat $emailBody |
mail -s "Hi, I'm looking for a position in IT Field." $emailTo |
echo “Success”;
done < <(grep . $emailList)
You are probably hitting a server-side limit on the number of messages you can send in a fixed time, or equivalently the number of connections allowed within a moving window of time.
If you can (the message is not "personalized") it is best to send one message to multiple recipients, rather than many messages, each to one recipient. Do that by perhaps putting your own e-mail address in the To field, and then Bccing the whole of the list of recipients in one go. You'll have to check your mail command for how to do that.

If either of the files exist then send email alert in bash shell script

Trying to mock a little shell script.. There is a process that already exists which creates one of the two files (i.e, file1 or file2). My goal is to be able to send email alert depending on which file is created.
For example, if file1 exists then send email to John, David, Smith stating that file1 was created. If file2 exists then send an email to the same group of people (John, David, Smith ) stating that file2 was created.
If I write a simple if-else it will work but I will need to repeat some parts of the code which I am trying to avoid.
#!/bin/bash
file="file1"
if [ -f "$file1" ]
then
send email to three people mentioned
else
send email to three people mentioned saying file2 was created since it always creates two files
fi
}
Here I am trying to construct a script in a better way and also I don't want to repeat "send email..." commands three times because I may need to add more people into the list in the future.
Please help. Thanks in advance.
You can create a function block that send a mail to a desired list of people.Call the function from either of the case
Something like
#!/bin/bash
userlist="$HOME/myusers.cfg" # one user per line
function send_alert {
while read -r user comment; do
echo "my command to send to ${user}"
echo "subject=$1"
echo "Content=Hello ${comment}\n$2"
done < "${userlist}"
}
file="file1"
if [ -f "$file1" ]; then
send_alert "My subject" "Content: ${file1} exists."
else
send_alert "My subject" "Content: ${file2} exists."
fi
I added an optional field comment in the myusers.cfg, so you can add some comment after a space (name person, something).
You can adjust send_alert for more business rules, such as only send once an hour an alert, only send during working hours except for the person on night shift, whatever.

mail format change when received

I have file.out logfile which contain plain text and formated as
1abcdefghijklmnopqrstuvwxyz
2abcdefghijklmnopqrstuvwxyz
3abcdefghijklmnopqrstuvwxyz
<--------------------
4abcdefghijklmnopqrstuvwxyz
5abcdefghijklmnopqrstuvwxyz
6abcdefghijklmnopqrstuvwxyz
My problem is when I received it in my mail box the format is changed
OUTPUT in MAILBOX
1abcdefghijklmnopqrstuvwxyz 2abcdefghijklmnopqrstuvwxyz 3abcdefghijklmnopqrstuvwxyz
<---------------
4abcdefghijklmnopqrstuvwxyz
5abcdefghijklmnopqrstuvwxyz
6abcdefghijklmnopqrstuvwxyz
the first 1,2,3 is not supposed to be in a single line. while the 4,5,6 stays formated as is.
the file was originally sent by perl script using Mail:Sendmail module, but I also tried sending through mail command to test also.
cat file.out | mail -s reportfile email#email.com got the same result.
I've also check the file and there is no extra spaces or characters.
MS Outlook 2007 issue. resolve it with this: go to Tools > Preferences > Email > Email-Options >Message handling > "Remove extra line breaks in plain text messages".

Create serial mail on linux commandline

I would like to generate serial emails on the linux commandline. Assume I have a file indicating mail address, subject and message text in columns on separate lines for each recipient. I.e.
recipient1#mail.com subject1 text1
recipient2#mail.com subject2 text2
...
The script should use standard commands as I intend to send it to colleagues who should create some emails for me.
The loop over the lines could be with xargs ... Can I use the commandline tool mail?
It is important that the mails are not send immediately. Ideally it creates files for import in the users preferred mail client. So that senders can check the mails before they submit.
I also would like to be able to add attachments to the Mails.
I tried e.g.
function mail_kmail {
kmail -s "$2" --body "$3" --attach "$4" --composer "$1"
}
function mail_thunderbird {
thunderbird -compose "to='$1',subject='$2',body='$3',attachment='$4'"
}
and reading the input data from file with
while read recipient subject body attach $file
do
mail_kmail "$recipient" "$subject" "$body" "$attach";
done
but this will work only if my colleagues installed and set up either of these mail clients.
I found this (closed) related question:
How can i send automated email in linux? .
You can use mutt to send the emails, here is an example:
echo "This is the message body" | mutt -a "/path/to/file.to.attach" -s "subject of message" -- recipient#domain.com
Since it's hard to know what you are trying to achieving here, you can even create a configuration file to be used, but you'll to investigate more or give more details about your use case.

Resources