當我用管道輸送貓郵寄郵件時,我遇到了一個奇怪的問題。
我有一個從腳本編譯而來的簡單文字文件,它是關於在腳本中複製了哪些資料夾的報告。然後我想透過電子郵件將此報告發送給系統管理員。
該腳本可以很好地產生報告。當我跑步時
cat /tmp/report.txt
我得到了格式良好的輸出,其中包含我期望的所有換行符。
Copy report. Started on Mon Sep 28 13:37:57 BST 2015
Run in progress?: Yes
3 directories were copied.
Copy started on 2015.09.28 at 13:37:57
Copying /mnt/smb/New folder (10) to /mnt/backup/New folder (10)
Copy completed on 2015.09.28 at 13:37:57
Copy info:
0.01user 0.47system 0:01.83elapsed 26%CPU (0avgtext+0avgdata 2668maxresident)k
0inputs+355896outputs (0major+150minor)pagefaults 0swaps
Copy started on 2015.09.28 at 13:37:59
Copying /mnt/smb/New folder (14) to /mnt/backup/New folder (14)
Copy completed on 2015.09.28 at 13:37:59
Copy info:
0.00user 0.41system 0:01.78elapsed 23%CPU (0avgtext+0avgdata 2620maxresident)k
0inputs+355896outputs (0major+152minor)pagefaults 0swaps
Copy started on 2015.09.28 at 13:38:01
Copying /mnt/smb/New folder (18) to /mnt/backup/New folder (18)
Copy completed on 2015.09.28 at 13:38:01
Copy info:
0.00user 0.28system 0:01.71elapsed 16%CPU (0avgtext+0avgdata 2768maxresident)k
0inputs+355896outputs (0major+151minor)pagefaults 0swaps
但是,由於某種原因,當我現在將其通過管道發送到郵件時
cat /tmp/report.txt | mail -s "Copy Report" [email protected]
或者
mail -s "Copy Report" [email protected] < /tmp/report.txt
這是我收到的電子郵件。
Copy report. Started on Mon Sep 28 13:37:57 BST 2015
Run in progress?: Yes
3 directories were copied.
Copy started on 2015.09.28 at 13:37:57
Copying /mnt/smb/New folder (10) to /mnt/backup/New folder (10) Copy completed on 2015.09.28 at 13:37:57 Copy info:
0.01user 0.47system 0:01.83elapsed 26%CPU (0avgtext+0avgdata 2668maxresident)k
0inputs+355896outputs (0major+150minor)pagefaults 0swaps
Copy started on 2015.09.28 at 13:37:59
Copying /mnt/smb/New folder (14) to /mnt/backup/New folder (14) Copy completed on 2015.09.28 at 13:37:59 Copy info:
0.00user 0.41system 0:01.78elapsed 23%CPU (0avgtext+0avgdata 2620maxresident)k
0inputs+355896outputs (0major+152minor)pagefaults 0swaps
Copy started on 2015.09.28 at 13:38:01
Copying /mnt/smb/New folder (18) to /mnt/backup/New folder (18) Copy completed on 2015.09.28 at 13:38:01 Copy info:
0.00user 0.28system 0:01.71elapsed 16%CPU (0avgtext+0avgdata 2768maxresident)k
0inputs+355896outputs (0major+151minor)pagefaults 0swaps
是什麼賦予了?有些換行符存在,有些則沒有!我嘗試在 vi 和 nano 中編輯文件,並確保換行符確實存在。也刪除它們並再次製作它們,電子郵件輸出保持不變。
使用 /usr/bin/mail
更多資訊:
我手動將以下內容輸入到文字檔案中,並對郵件執行相同的操作。我將“複製”一詞放在每行的開頭。
Copy started on 2015.09.28 at 13:37:57
Copying /mnt/smb/New folder (10) to /mnt/backup/New folder (10)
Copy completed on 2015.09.28 at 13:37:57
Copy info:
Copy 0.01user 0.47system 0:01.83elapsed 26%CPU (0avgtext+0avgdata 2668maxresident)k
Copy 0inputs+355896outputs (0major+150minor)pagefaults 0swaps
這是我在電子郵件中收到的內容:
Copy started on 2015.09.28 at 13:37:57
Copying /mnt/smb/New folder (10) to /mnt/backup/New folder (10) Copy completed on 2015.09.28 at 13:37:57 Copy info:
0.01user 0.47system 0:01.83elapsed 26%CPU (0avgtext+0avgdata 2668maxresident)k
0inputs+355896outputs (0major+150minor)pagefaults 0swaps
然後我刪除了所有“複製”字樣。這是我在電子郵件中收到的內容。
Started on 2015.09.29 at 09:46:49
Copying /mnt/smb/New folder (10) to /mnt/backup/New folder (10) Completed on 2015.09.29 at 09:46:49
Info:
0.00user 0.46system 0:01.98elapsed 23%CPU (0avgtext+0avgdata 2552maxresident)k
0inputs+355896outputs (0major+149minor)pagefaults 0swaps
認為這可能與“複製”一詞有關,但我仍然有一行沒有換行符!
這是的輸出cat -vet /tmp/report.txt
Copy report. Started on Mon Sep 28 13:37:57 BST 2015$
$
Run in progress?: Yes$
$
$
$
3 directories were copied.$
$
$
Copy started on 2015.09.28 at 13:37:57$
Copying /mnt/smb/New folder (10) to /mnt/backup/New folder (10)$
Copy completed on 2015.09.28 at 13:37:57$
Copy info:$
0.01user 0.47system 0:01.83elapsed 26%CPU (0avgtext+0avgdata 2668maxresident)k$
0inputs+355896outputs (0major+150minor)pagefaults 0swaps$
$
$
Copy started on 2015.09.28 at 13:37:59$
Copying /mnt/smb/New folder (14) to /mnt/backup/New folder (14)$
Copy completed on 2015.09.28 at 13:37:59$
Copy info:$
0.00user 0.41system 0:01.78elapsed 23%CPU (0avgtext+0avgdata 2620maxresident)k$
0inputs+355896outputs (0major+152minor)pagefaults 0swaps$
$
$
Copy started on 2015.09.28 at 13:38:01$
Copying /mnt/smb/New folder (18) to /mnt/backup/New folder (18)$
Copy completed on 2015.09.28 at 13:38:01$
Copy info:$
0.00user 0.28system 0:01.71elapsed 16%CPU (0avgtext+0avgdata 2768maxresident)k$
0inputs+355896outputs (0major+151minor)pagefaults 0swaps$
答案1
您可能正在遭受“format=flowed”的困擾特徵很早以前就介紹過了,參見RFC2646。
您可能會發現某些行具有尾隨空格字元。一些郵件讀者將其解釋為「將此行與下一行連接起來,然後重新進行文字環繞以使行適合螢幕寬度」。
Content-Type
通常,只有當您的郵件具有header 選項時才應執行此操作format=flowed
,但如今,由於非 Unix 人群,這已成為預設值。
使用cat -vet
您的資料來查看是否存在尾隨空格。用於sed 's/ \+$//'
刪除它們。
我在您的資料中沒有看到任何尾隨空格或控製字元而不是回車符,因此您的郵件程式沒有真正的理由篡改它。
最後一次嘗試:正如 Andrzej A. Filip 建議的那樣,不要mail
直接訪問sendmail
.這將允許您新增顯式內容類型標頭。例如:
(echo 'Subject: Copy Report'
echo 'Content-Type: text/plain; charset=us-ascii; format=fixed'
echo
cat /tmp/report.txt
) | sendmail [email protected]
最後echo
是在數據之前結束標頭。 sendmail 可能不在您的 PATH 中,請查看 /sbin 和 /usr/sbin 等。