命令行发送邮件设置参考

描述命令行发送邮件相关设置,如有需求让任务在后台运行并在结束后发送通知,可参考

1. 安装软件

如下命令安装 heirloom-mailx, 安装过程自动安装s-nail


apt install heirloom-mailx
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  s-nail
Suggested packages:
  exim4 | mail-transport-agent
The following NEW packages will be installed:
  heirloom-mailx s-nail
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 508 kB of archives.
After this operation, 1,064 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 https://mirrors.ustc.edu.cn/parrot parrot/main i386 s-nail i386 14.9.5-1 [498 kB]
Get:2 https://mirrors.ustc.edu.cn/parrot parrot/main i386 heirloom-mailx all 14.8.16-1 [9,640 B]
Fetched 508 kB in 10s (48.4 kB/s)
Selecting previously unselected package s-nail.
(Reading database ... 198935 files and directories currently installed.)
Preparing to unpack .../s-nail_14.9.5-1_i386.deb ...
Unpacking s-nail (14.9.5-1) ...
Selecting previously unselected package heirloom-mailx.
Preparing to unpack .../heirloom-mailx_14.8.16-1_all.deb ...
Unpacking heirloom-mailx (14.8.16-1) ...
Setting up s-nail (14.9.5-1) ...
Processing triggers for menu (2.1.47+b1) ...
Processing triggers for man-db (2.7.6.1-2) ...
Setting up heirloom-mailx (14.8.16-1) ...
Configuring sandbox profiles....
Sandbox profiles updated!

2. 配置软件
在 /etc/s-nail.rc的末尾增加配置邮箱参数


set imap-use-starttls
set pop3-use-starttls
set smtp-use-starttls

set [email protected]
set smtp=smtp.mymail.com
set [email protected]
set smtp-auth-password=MyPassword
set smtp-auth=login

前面三行关于tls的根据各自情况配置,后几行配置邮箱的发送地址、smtp、帐号密码等信息。

3. 发送邮件

发送邮件使用 heirloom-mailx 命令,具体参数参考man,样例:


echo "a test mail from console" | heirloom-mailx -s "test console mail"  [email protected]

4. 参考脚本

涉及自动发送时,可包装至简单脚本中供调用,脚本主要优化处理message中多行情况。

 cat /path/autosendmail.sh

#!/bin/sh
messages=`echo $3 | tr '\r\n' '\n'`
subject=`echo $2 | tr '\r\n' '\n'`
echo "${messages}" | heirloom-mailx -s "${subject}" $1 >>/tmp/sendmail.log 2>&1
echo $1 >>/tmp/sendmail.log 2>&1
echo $2 >>/tmp/sendmail.log 2>&1
echo $3 >>/tmp/sendmail.log 2>&1
echo `date` >>/tmp/sendmail.log 2>&1
echo "" >>/tmp/sendmail.log 2>&1

调用方法:

./autosendmail.sh somebody@somewhere  "this is a test subject"  "this is Message"
1 个赞

发送邮件的话sendmail这个软件也可以,好像还可以伪造发件人来着

1 个赞

服务器资源由ZeptoVM赞助

Partners Wiki Discord