Generate E-Mails with content on different files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Generate E-Mails with content on different files
# 1  
Old 01-06-2017
Question Generate E-Mails with content on different files

Hello,

I am using a script to create a random password for a user.
The file looks like
Code:
 
user1:thepassword1  
user2:thepassword2  
user3:thepassword3

Passwords are openssl passwd hashed. I have an own file where the password is written in cleartext.
Now i want to send an E-Mail to the users with the new password.
I have a file (maillist.txt) with

Code:
 
user1:mail@domain1.tld  
user2:mail@domain2.tld  
user3:mail@domain3.tld

Now i want to generate an E-Mail to user1.
This should generate an E-Mail to mail@domain1.tld, containing the username (user1) and the password (password1). Automatic for each user.
I want to do it with mailx.
Can you please help me?
Regards
Manu



Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by Manu_H; 01-06-2017 at 01:09 PM.. Reason: Added CODE tags.
# 2  
Old 01-06-2017
Hashing is not reversible, the hash file is useless for anything but verifying a password someone gives to you.

Code:
while IFS=":" read USERID EMAIL
do
        PASS="$(awk -F: '$1 == USERID { print $2 }')
        if [ -z "$PASS" ]
        then
                echo "No pass for user $USERID" >&2
                continue
        fi

        mailx -s "password for $USERID" EMAIL <<EOF
From:  someone@myemailserver
Subject:  Automatic password
Your automatically generated password is $PASS
EOF

        [ "$?" -eq 0 ] || echo "Couldn't email password for $USERID" >&2
done < emailfile

Test this with example data before you try it for real.
# 3  
Old 01-06-2017
Question

Thank you.
I' am using this Script right now...
Code:
join -t: cleartext.txt maillist.txt | awk -F: '{ print "echo Username: "$1" Passwd: "$2" | mailx -s Information "$3 }' |sh

$1 is the Username from Cleartext.txt
$2 is the Password from Cleartext.txt
$3 is the email from the user in maillist.txt
This works fine like this, but i want to add more text (Something like "Hello, your password has been..."). But if i do this, i get an error: i get an unexpected error.
Script from above changed to... { print "echo Blablabla \n test Username: ...}
# 4  
Old 01-06-2017
An awk script which generates shell commands is probably not the greatest idea, it would be a security and quoting nightmare. It's not the use of awk or sh, as much as the way they're joined, that's the problem. What if someone's password happens to be '" ; sleep 900000 ? That would be fun. Handle the reading yourself, so that there's no opportunity for arbitrary execution.

I'd forgotten about join, though, that simplifies my script a lot:

Code:
join -t: cleartext.txt maillist.txt | while IFS=":" read -r USERID PASS EMAIL
do
        mailx -s "password for $USERID" EMAIL <<EOF
From:  someone@myemailserver
Subject:  Automatic password
Your automatically generated password is $PASS
This email can contain as many lines as you want
etc
etc
EOF
done


Last edited by Corona688; 01-06-2017 at 05:10 PM.. Reason: fix typo: joint/join
# 5  
Old 01-06-2017
Hey, thats perfect! Thanks.
Is there just a possibilty to set to "TO" E-Mail to the Adress from the maillist.txt?
Currently all Mails are send to correct recipient, but the "TO" Field is "EMAIL@hostname"@domain.tld.
And where do you define the Variables? (Like $PASS, $USERID etc?)

I edited the Script by adding a -r, to show an sender mail.

Code:
join -t: cleartext.txt maillist.txt | while IFS=":" read -r USERID PASS EMAIL
do
        mailx -r "donotreply@domain.tld" -s "password for $USERID" EMAIL <<EOF
Hello $USERID,

Your automatically generated password is $PASS
EOF
done


Last edited by Manu_H; 01-06-2017 at 05:04 PM..
# 6  
Old 01-06-2017
read defines them, setting them for every line read. It takes the variable NAME, not the variable itself, which is why its read -r VARNAME and not read -r $VARNAME

Try putting it in with the email, like:

Code:
mailx -s "password for $USERID" EMAIL <<EOF
To:  $EMAIL
From:  someone@myemailserver
Subject:  Automatic password

Your automatically generated password is $PASS
This email can contain as many lines as you want
etc
etc
EOF

p.s. Do not indent the email contents to make them look prettier in the script, it won't work right if you indent it, especially the ending EOF. That has to be at the literal beginning of the line to properly end the here-document.
# 7  
Old 01-06-2017
Actually not.
The Mail is still from "EMAIL@hostname"@domain.tld
The E-Mail just looks like:

Code:
To:  test@manuh.xyz 
From: someone@myemailserver   
Subject:  Automatic password    
Your automatically generated password is test   
This email can contain as many lines as you want  
etc   
etc

Normally i use mailx like that.
mailx -r sender@domain.tld -s "Mail Subject" recepient@domain.tld

I found another way..
How would this be (security`?)

Code:
#!/bin/bash
>tmp.txt
join -t: cleartext.txt maillist.txt | while read line
do
  echo $line
  USER=$(echo $line | awk -F: '{ print $1 }')
  PSWD=$(echo $line | awk -F: '{ print $2 }')
  EMAILID=$(echo $line | awk -F: '{ print $3 }')
  echo "Hello $(echo $USER | tr a-z A-Z), your password has been changed!!!<br>" >tmp.txt
  echo "<b>Username</b>: $USER<br>" >>tmp.txt
  echo "<b>Password</b>: $PSWD<br>" >> tmp.txt
  cat tmp.txt | mailx -s "$(echo -e 'Password Changed!!!\nContent-Type: text/html')" $EMAILID
done


Last edited by Manu_H; 01-06-2017 at 05:21 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Umask to generate files with rwx permissions for all

i need my script1.sh to generate /tmp/temp.txt with full permissions i.e -rwxrwxrwx 1 user1 users 23 Dec 16 10:52 /tmp/temp.txtmore script1.sh umask 666 echo "hello">/tmp/temp.txt But the script1.sh generates temp.txt with different permissions as shown below. -rwxr-xr-x 1 user1 users... (6 Replies)
Discussion started by: mohtashims
6 Replies

2. Shell Programming and Scripting

How to match in 2 files and generate 3rd file?

Hello, I have 2 tables (first file with colon separated, second file comma separated) like below: Please note that the matching number (kind of primary key) is a number and is NOT unique. It is 2nd column in table1 and 4th column in table2. # cat table1 vgbpjdata1:80 vgbpjdata2:50... (6 Replies)
Discussion started by: reddyr
6 Replies

3. Shell Programming and Scripting

Merge files and generate a resume in two files

Dear Gents, Please I need your help... I need small script :) to do the following. I have a thousand of files in a folder produced daily. I need first to merge all files called. txt (0009.txt, 0010.txt, 0011.txt) and and to output a resume of all information on 2 separate files in csv... (14 Replies)
Discussion started by: jiam912
14 Replies

4. Shell Programming and Scripting

Script for count files and send mails

Hi. I'm new on this forum and I need if possible someone to help me with one script. The script should act like this: - should be run by crontab and have next parameters: script_name $par1 $par2 $par3 $par4 where script will search in dir $par1 for files with mask $par2 and if number of... (2 Replies)
Discussion started by: atrailm
2 Replies

5. UNIX for Dummies Questions & Answers

Generate list of deleted files

I copied all JPEGs from my laptop to an external drive using find . -name "*.jpg" -exec cp '{}' ./media/Backup/pictures \; And then deleted all of them from my laptop. Now, I realize that I need the folder path of all the original JPEGs as the path has the important information. I dont... (1 Reply)
Discussion started by: eshwaconsulting
1 Replies

6. Shell Programming and Scripting

Script to consolidate files as mails,groupid

Hi, I'm newbie in Bash scripting. Here is scenario I've a file which containslist of 100 mailgroup and in a directory i have 100 files with each group's respective mail addresses. I'd like to make one CSV file with this information as follows. consolidated files with mailid,groupname. ... (7 Replies)
Discussion started by: manish123456
7 Replies

7. Shell Programming and Scripting

Generate a change list of files/dir

Is there a tool that can diff a directory and generate a change list of files in that directory based on a previous snapshot on the directory? For example /etc/a.txt:changed /etc/b.txt:removed /etc/c.txt:added Thanks! (1 Reply)
Discussion started by: overmindxp
1 Replies

8. Shell Programming and Scripting

script to move files as they generate to another server ?

Im trying to come up with a way to automate moving logs as they generate from one server to another. Anyone have script or idea how most simply to get it done ? want to get this working so dont have to monitor and move these logs during the evening. Thanks for any ideas -Vinman (2 Replies)
Discussion started by: vinman
2 Replies

9. Shell Programming and Scripting

Help generate report from log files

Hi Expert, I have some confusing to generate report from the log file as shown below: filename :test1.log start_time date end_time number code P 000029.621 20070823 000029 12134567890 111111111111 00 0 000 003... (4 Replies)
Discussion started by: bucci
4 Replies

10. Shell Programming and Scripting

how to generate a list of files

Hello people I need to find a way to generate a file that contains the names of all *.jpg files that were generated after a specific date The search should start in my current folder and recursively search inner folders It would be best to list the file names one below the other in the output... (3 Replies)
Discussion started by: jasongr
3 Replies
Login or Register to Ask a Question