在Ubuntu上定时发送邮件可通过以下步骤实现,核心工具为crontab(定时任务)+ 邮件工具(如mailx、ssmtp等):
选择一种邮件工具并安装:
sudo apt install mailutils # 安装mailutils(包含mailx)
sudo apt install ssmtp mpack # mpack用于发送附件
mail -s "主题" recipient@example.com < body.txt发送。/etc/ssmtp/ssmtp.conf,填入SMTP服务器信息(如Gmail):root=postmaster
mailhub=smtp.gmail.com:587
AuthUser=your_email@gmail.com
AuthPass=your_app_password # 使用应用专用密码
UseSTARTTLS=YES
创建一个Shell脚本(如send_email.sh),内容示例:
#!/bin/bash
# 邮件内容
SUBJECT="定时邮件测试"
BODY="这是通过Ubuntu定时发送的邮件内容,当前时间:$(date)"
RECIPIENT="recipient@example.com"
# 使用mailx发送
echo "$BODY" | mail -s "$SUBJECT" "$RECIPIENT"
# 若需发送附件,使用ssmtp+mpack(需先安装)
# echo "邮件正文" | mpack -s "$SUBJECT" -a "附件路径" "$RECIPIENT"
注意:脚本需赋予可执行权限:
chmod +x send_email.sh
crontab -e
分 时 日 月 周 命令):# 每天上午10点发送邮件
0 10 * * * /path/to/send_email.sh
# 每5分钟测试一次(调试用)
*/5 * * * * /path/to/send_email.sh
sudo service cron restart
sudo tail -f /var/log/syslog | grep cron
/home/user/script.sh)。chmod 700防止未授权访问)。以上步骤参考自,可根据实际需求选择邮件工具和配置方式。