今天做了一个小脚本,要实现的功能如下
一个是用户信息文件
# cat file1
第一个位置#皮皮鲁#第二个位置#0101#第三个位置#男#xxx@126.com
第一个位置#鲁西西#第二个位置#0101#第三个位置#女#xxx@163.com
......

一个是模板文件
# cat file2
用户姓名#1#用户生日#2#用户性别#3#填写完毕

要把file1中的#...#分割的内容,分别替换到file2中“#1#”,“#2#”,“#3#”的位置,然后发送邮件到file1中的最后一列的邮箱地址中,下面是脚本内容:
# cat sendfile.sh
#!/bin/sh
cd /home/shell
tmpfile=`cat file2`

cat file1 |while read line
do
   str1=`echo $line |awk -F "#" '{print $2}'`
   str2=`echo $line |awk -F "#" '{print $4}'`
   str3=`echo $line |awk -F "#" '{print $6}'`
   mailuser=`echo $line|awk -F "#" '{print $7}'`

   tmp=`echo $tmpfile|sed -e "s/#1#/${str1}/g"`
   tmp=`echo $tmp|sed -e "s/#2#/${str2}/g"`
   tmp=`echo $tmp|sed -e "s/#3#/${str3}/g"`
   #echo $tmp   //得到替换后的文件内容

   /usr/local/bin/sendEmail -f xxx@126.com -t "$mailuser" -s mail.126.com -u "string mail" -xu xxx@126.com -xp 123456 -m "$tmp"  -o message-charset=GB2312
   sleep 20s
done


开始的时候发送邮件,邮件的内容中文总是显示乱码,查看信头,编码格式为
charset="iso-8859-1"
编辑了无数次file2文件都无效,后来无意中,sendEmail报了错误
Synopsis:  sendEmail -f ADDRESS [options]
  
  Required:
    -f ADDRESS                from (sender) email address
    * At least one recipient required via -t, -cc, or -bcc
    * Message body required via -m, STDIN, or -o message-file=FILE
    
  Common:
    -t ADDRESS [ADDR ...]     to email address(es)
    -u SUBJECT                message subject
    -m MESSAGE                message body
    -s SERVER[:PORT]          smtp mail relay, default is webmail.xywy.com:25
    
  Optional:
    -a   FILE [FILE ...]      file p_w_upload(s)
    -cc  ADDRESS [ADDR ...]   cc  email address(es)
    -bcc ADDRESS [ADDR ...]   bcc email address(es)
    -xu  USERNAME             username for SMTP authentication
    -xp  PASSWORD             password for SMTP authentication
    
  Paranormal:
    -b BINDADDR[:PORT]        local host bind address
    -l LOGFILE                log to the specified file
    -v                        verbosity, use multiple times for greater effect
    -q                        be quiet (i.e. no STDOUT output)
    -o NAME=VALUE             advanced options, for details try: --help misc
        -o message-file=FILE         -o message-format=raw
         -o message-header=HEADER     -o message-charset=CHARSET
        -o reply-to=ADDRESS          -o timeout=SECONDS
        -o username=USERNAME         -o password=PASSWORD
        -o tls=<auto|yes|no>         -o fqdn=FQDN
  
  Help:
    --help                    the helpful overview you're reading now
    --help addressing         explain addressing and related options
    --help message            explain message body input and related options
    --help networking         explain -s, -b, etc
    --help output             explain logging and other output options
    --help misc               explain -o options, TLS, SMTP auth, and more

原来sendEmail可以设置字符编码的,于是加上了
-o message-header=GB2312
就OK了~~~!!!!