Skip to content

Instantly share code, notes, and snippets.

@1syo
Created January 7, 2014 08:39
Show Gist options
  • Save 1syo/8296372 to your computer and use it in GitHub Desktop.
Save 1syo/8296372 to your computer and use it in GitHub Desktop.
#! /bin/sh
host=localhost
database=db
user=dbuser
password=password
from_date=`date --date '1 days ago' +\%Y-\%m-\%d`
to_date=`date +\%Y-\%m-\%d`
[email protected]
[email protected]
subject=counter
bound=`date +"%Y%m%d%H%M%S"`
filename="filename_$from_date.csv"
sql="SELECT * FROM users WHERE created_at BETWEEN $from_date AND $to_date"
result=`mysql -u$user -p$password -h$host $database -e"$sql" | sed 's/\t/","/g;s/^/"/;s/$/"/;s/\r\n//g'`
/usr/sbin/sendmail -t -i <<__MAIL__
From: "$from"
To: "$to"
Subject: "$subject"
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=\"---------"$bound"\"
-----------"$bound"
Content-Type: Text/Plain; charset=ISO-2022-JP
Content-Transfer-Encoding: 7bit
Now: "$from_date"
--
`hostname`
"$from"
-----------"$bound"
Content-Type: application/octet-stream
name="$filename"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="$filename"
`echo -e "$result" | base64`
-----------"$bound"--
__MAIL__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment