是的,Ubuntu 定時任務(cron)可以用來定時發送郵件。你可以使用命令行郵件客戶端(如 mail 或 sendmail)或腳本結合 cron 來實現這一功能。
以下是一個簡單的示例,展示如何使用 cron 和 mail 命令定時發送郵件:
安裝郵件客戶端(如果尚未安裝):
sudo apt-get update
sudo apt-get install mailutils
配置郵件發送功能:
編輯 /etc/mail.rc 文件,添加你的郵件服務器信息:
set smtp=smtp.yourmailserver.com
set smtp-auth=login
set smtp-auth-user=your-email@example.com
set smtp-auth-password=your-password
set from=your-email@example.com
創建一個發送郵件的腳本:
創建一個名為 send_email.sh 的腳本文件:
nano ~/send_email.sh
在文件中添加以下內容:
#!/bin/bash
echo "This is the body of the email" | mail -s "Subject of the email" recipient@example.com
保存并退出編輯器,然后賦予腳本執行權限:
chmod +x ~/send_email.sh
設置定時任務:
使用 crontab -e 命令編輯當前用戶的 crontab 文件:
crontab -e
添加一行來設置定時任務,例如每天早上 8 點發送郵件:
0 8 * * * /home/yourusername/send_email.sh
保存并退出編輯器。
這樣,系統就會在每天早上 8 點自動執行 send_email.sh 腳本,發送一封郵件。
請注意,使用明文密碼存儲在腳本或配置文件中是不安全的。建議使用更安全的方法,如環境變量或加密存儲密碼。