Shell日常实践之批量发送邮件

需求:利用mail命令向朋友们批量发送聚会的邀请,称呼(或者还有其他内容)因人而异。

Preliminaries:本地系统已配置好email服务,具体配置可参考

http://blog.csdn.net/zhuying_linux/article/details/7091688

http://blog.csdn.net/hitabc141592/article/details/25986911

方案1:直接采用for循环

脚本文件email如下:

#!/bin/bash
for name in Peter Mike
do
echo "Dear $name,

Hi! I'm glad to inform you that there will be a party this Friday night at my place. I'd appreciate it if you could come.

Sincerely,
Kinosum" | mail -s "Invitation" [email protected]
done
  评论:如果除了称呼,还要引入其他变量(比如说上面的邮箱地址的域名不都是null.com),则需要多个for循环嵌套,但这种穷举可能效率比较低下。

方案2:利用事先编制好的列表

我们可以先建立一个文件'list',每一行为一个记录,根据需求分成多个字段,为了和字段中可能出现的空白符相区分,我们就用逗号‘,’作为分隔符。建立好的文本文件形如(每行依次为称呼,关系和邮箱地址):
Peter,brother,[email protected]
Mike,colleague,[email protected]

然后,脚本文件email如下:
#!/bin/bash
cat list | while read line
do
name=$(echo $line | cut -d "," -f 1)
identity=$(echo $line | cut -d "," -f 2)
email=$(echo $line | cut -d "," -f 3)
echo "Dear $name,

Hi! I'm glad to inform my beloved $identity you that there will be a party this Friday night at my place. I'd appreciate it if you could come.

Sincerely,
Kinosum" | mail -s "Invitation" $email
done

你可能感兴趣的:(Shell日常实践之批量发送邮件)