我有一个如下所示的BASH脚本:
cat limit_mail.out | while read num email limit orders; do
echo "Sending mail to '$email'"
printf "$email_template" "$email" "$num" "$limit" "$orders" |
sendmail -oi -t
done
如何进行此操作,以便在发送电子邮件时,将电子邮件地址与日期和时间一起保存在文本文件中,然后进行检查,以确保没有电子邮件地址收到超过1 e- 24小时内邮寄?
答案 0 :(得分:2)
一种方法是为每个收件人创建一个文件,并使用该文件的时间戳。
MAIL_TIMESTAMPS=/var/cache/mailstamps
mkdir "$MAIL_TIMESTAMPS"
cat limit_mail.out | while read num email limit orders; do
echo "Sending mail to '$email'"
email_hash="$(md5sum <<< "$email" | cut -d' ' -f1)";
# Check that a timestamp file doesn't exist, or that it was modified over 24h ago
if ! test -n "$(find "$MAIL_TIMESTAMPS" -mtime -1 -name "$email_hash")"; then
touch "$MAIL_TIMESTAMPS/$email_hash" # Update timestamp
printf "$email_template" "$email" "$num" "$limit" "$orders" |
sendmail -oi -t
fi
done
修改:我已添加了电子邮件地址的散列。无论如何,这是我打算做的事情,但是Aleks-Daniel's code for that非常简洁,以至于我在这里借了它,只从sha256sum改为md5sum。 MD5速度更快,虽然它有潜在的问题我不认为它们会成为问题(当然你可以自由选择)。哈希还避免了特殊字符的问题,这会扰乱查找文件名的匹配。
答案 1 :(得分:1)
在文件中使用时间戳:
DELAY_FOLDER='myTempFolder/'
DELAY=$((24*60*60)) # one day
while read num email limit orders; do
echo "Sending mail to '$email'"
if [[ -f $DELAY_FOLDER/$email ]] && (( $(cat "$DELAY_FOLDER/$email") + DELAY > $(date +%s) )); then
echo "email has been sent already"
else
printf "$email_template" "$email" "$num" "$limit" "$orders" | sendmail -oi -t
echo "$(date +%s)" > "$DELAY_FOLDER/$email"
fi
done < limit_mail.out
此外,如果您不希望任何人查看临时文件夹中的电子邮件地址,您可以使用md5或sha sum来覆盖您的地址。像这样:
DELAY_FOLDER='myTempFolder/'
DELAY=$((24*60*60)) # one day
while read num email limit orders; do
echo "Sending mail to '$email'"
emailsha=$(sha256sum <<< "$email" | cut -d' ' -f1)
if [[ -f $DELAY_FOLDER/$emailsha ]] && (( $(cat "$DELAY_FOLDER/$emailsha") + DELAY > $(date +%s) )); then
echo "email has been sent already"
else
printf "$email_template" "$email" "$num" "$limit" "$orders" | sendmail -oi -t
echo "$(date +%s)" > "$DELAY_FOLDER/$emailsha"
fi
done < limit_mail.out