Send email from shell to users in Mysql


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Send email from shell to users in Mysql
# 1  
Old 12-23-2011
Send email from shell to users in Mysql

Hi, I need help about sending mail in linux. I have a ubuntu server with working web site, users are uploading files in one main directory (to folder inbox). With samba I shared that directory and on other mashine is Windows server and application that is reading that files and writting content to database. Based of the result is creating new file with content: package name that is recieved, size, date and time and does the package contained erros or it was OK. That newly created files are again sent over samba to the same directory in ubuntu server (to folder outbox). That all is working!

Now, i need to listen are there new files in folder outbox. If there are new files, I need to send their content to specific email address. I have users in Mysql database. The name of files that are coming to outbox is with mask like this ddmmyyyy.x where x is user id in Mysql. So if "extension" of file is 1, I need to send mail to mail address of user in mysql with user id 1. If its 2 I am sending to user with user id... and after that move file from outbox to archive.

So, it's going like this: new file arrived in outbox, based on the extension of that file, select email address of user from database with user id = extension of file. On that email address send content of that file and move file to archive.

Does anyone can help me to solve this. Thank you!

P.S I have a mail working and I can send email from shell.
# 2  
Old 12-23-2011
Anything you tried yourself? Please show some code using code tags, thanks. There should be many examples in this forum here if you use the search function to get an easy start.
# 3  
Old 12-24-2011
I done this so far, I start script manualy and it going through a directory. If there is file with extension I want I move it to archive and calling a php file that is sending mail to that user. Problem is here that I start script manualy and I second problem is that file I include in php like a message in mail i define in code, it's not text from file that called that code. I don't know how to solve this because I am begginer in linux and not so good in shell programming. Here is my code:

Code:
#!/bin/sh
# Configure variables at the begining of the script
MONITOR_DIR=/var/www/frep/predaja2
TMP_DIR=/home/tmp
SUCCESS_DIR=/home/t_arhiva
FAILED_DIR=/home/g_arhiva
cd $MONITOR_DIR
COUNT_FILES=$(ls -l * | grep ^- | wc -l)
if [ $COUNT_FILES -gt 0 ]; then
echo "A total of" $COUNT_FILES "file(s) found.";

for f in *.562; do
php /var/www/mail/mail.php
echo "Moving file:" $f "to:" $TMP_DIR/$PROCCESS_ID_DIR;
mv $f $TMP_DIR/$PROCCESS_ID_DIR
mv $f $SUCCESS_DIR/
done
else
echo "No files found... Exit!";
fi

PHP Code:
<?php
//orderid = 777;
$dbhost 'localhost';
$dbuser =&#8217;’xxx;
$dbpass 'xxxxxxx';
$dbname 'xxxx';
$conn mysql_connect($dbhost$dbuser$dbpass);
if(!
$conn) { die('Failed to connect to server: ' mysql_error()); }
mysql_select_db($dbname);
$query=mysql_query("SELECT email FROM users WHERE userID = '777'");
$query_row=mysql_fetch_array($query);
$strfile_get_contents("d13121101.562");
$proba = ($query_row[email]);
$to $proba;
$subject "Test mail";
$message =  $str;
$from "info@xxx.net";
$headers "From:" $from;
mail($to,$subject,$message,$headers);
echo 
"Mail Sent.";
?>
# 4  
Old 12-26-2011
Assuming that you want to run this as a cron job eventually, you should add an outer loop to the sh script.
Code:
#!/bin/sh
if [ -r job.pid ]
then
echo previous job `cat job.pid` still running
exit
fi
echo $$ >job.pid
.....your script
rm job.pid

This will prevent a second job starting while the first is still running (or has died.)
Secondly, the line "for f in *562" will pick partially received files if this process is running while some other process is dropping a new file into the directory.
This User Gave Thanks to jgt For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with shell script to send email once

Hi Guys, I have this script which will monitor oracle db process if up or down.And I want it to send email if it's down and the time it's back to online. However my script just keep on sending "Email Up" if the db is up or "Email Down" if the db is down.Is there any way to trap it so that it... (5 Replies)
Discussion started by: d3xt3r
5 Replies

2. Shell Programming and Scripting

Script to send email after comparing the folder permissions to a certain permission & send email

Hello , I am trying to write a unix shell script to compare folder permission to say drwxr-x-wx and then send an email to my id in case the folders don't have the drwxr-x-wx permissions set for them . I have been trying to come up with a script for few days now , pls help me:( (2 Replies)
Discussion started by: nairshar
2 Replies

3. Shell Programming and Scripting

How to send email through shell script

Hi All, I am new to the unix , i have to deliver one script very urgently I have to write a shell script where i have i want to send email to specific email id in this script i want FROM to be parameterized and stored in a variable TO to be parameterized and stored in a variable... (3 Replies)
Discussion started by: nileshbhawsar
3 Replies

4. Shell Programming and Scripting

Send Email using Korn Shell

Hi All, I need assistance is sending email out using korn shell Steps, 1- Count number of records in database. 2- if count is more than 2000 3- send email to user else if less then 1999 exit out. Here is my script and is not exiting out and need to press .DOT and Enter command to... (2 Replies)
Discussion started by: atlurip
2 Replies

5. Shell Programming and Scripting

make un shell script to send email

Hi, Someone Knows how to obtein a chain from a unix file, but not all the line, for exemple, for this file ,obtein only 902111111 and to keep the value, this value will be used to make a mailx. NF=ALL, SUBJ= FROM: SN=CD, SE=TOPCALL, NA=, N=902111111, N=TCLFI TO: SE=FAX, NA=, C1=... (0 Replies)
Discussion started by: peybol
0 Replies

6. Shell Programming and Scripting

shell send html email

I know how to send an email with sendmail in a shell script. I know how to send an email with an attachment in a script. But im trying to send an email and need to set Content-Type to text/html and insert a file as the body and not attachment. Send email with file as attachment: ... (4 Replies)
Discussion started by: Ikon
4 Replies

7. UNIX for Dummies Questions & Answers

to send email to multiple users

hi, i'm pretty new to this unix. i've been asked to create a shell script which will pick up the email id from a text file(stored in same machine, same directory) searches for that id in another file in which a product name( a one line text) is mentioned against it. then it should send a mail... (0 Replies)
Discussion started by: vishwas.shenoy
0 Replies

8. Shell Programming and Scripting

How to write a shell script to send an email to an id

Hi all, I want to know how to send an email using shell script ..... I tried a command call mail but it did not work.. Please any one reply.. With example if you know thanks.. Madhu (4 Replies)
Discussion started by: madhumathikv
4 Replies

9. UNIX for Dummies Questions & Answers

send email to more users

Hi, If I want to send an email to serveral users, what is the correct way to do it? I know a little bit mail command. If I want to send email to John and Scott after UNIX shell scripts have completed the job. I type: mail -s 'Job has been completed successfully' john@yahoo.com < log.txt ... (4 Replies)
Discussion started by: duke0001
4 Replies

10. Shell Programming and Scripting

Using mailx to send email to multiple users.

Hi, I am using the mailx command to send email to multple users. The command works fine when i am sending mail to a single user but when i insert multiple email ids inside the quote it does not work. All the email ids are coming from a property file.Please have a lookt at the property file and... (4 Replies)
Discussion started by: priyaksingh
4 Replies
Login or Register to Ask a Question