mailx - 邮件发送
mailx
配置邮件帐号:/etc/mail.rc
set from=youemail@abc.com
set smtp=smtp.abc.com
set smtp-auth-user=mymon
set smtp-auth-password=111111
set smtp-auth=login
命令参数
mailx -eiIUdEFntBDNHRV~ -T FILE -u USER -h hops -r address -s SUBJECT
-a FILE -q FILE -f FILE -A ACCOUNT -b USERS -c USERS -S OPTION users
简单地发送
echo "hello word" | mail -v -s " mailx test" youemail@abc.com
# 从文件读取要发送的邮件内容
cat body.txt | mail -v -s " mailx test" youemail@abc.com
mail -q body.txt -v -s " mailx test" youemail@abc.com
# 发送附件
echo "hello word" | mail -a body.txt -v -s " mailx test" youemail@abc.com
env MAILRC=/dev/null from=scriptreply@domain smtp=host \
smtp-auth-user=login smtp-auth-password=secret \
smtp-auth=login mailx -n -s "subject" \
-a attachment_file recipient@domain
true | mailx -n -a MYTEXT.txt -r my@mail.com -s log -S smtp=mail.com -S smtp-auth-user=MYUSER -S smtp-auth-password=MYPASSWORD FRIEND@mail.com
用sendmail发送html邮件
#!/bin/sh
SUBJECT='test mail'
RECIPIENT='user@domain.com'
FILE='TEST.html'
cat - $FILE <
MIME-Version: 1.0
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 8bit
Subject: $SUBJECT
To: $RECIPIENT
END
用mailx发html邮件
# 另还可用 -S sendcharsets=utf-8参数,但测试并无效果。
# 没有LC_CTYPE设置,中文乱码。
# 文件以附件方式加入,html展示成功,中文正常
export LC_CTYPE=zh_CN.utf-8;echo ""|mailx -a ngxszqWchat.html -s testmail10 -v youmail@abc.com
# 文件以附件方式加入,同时有body内容,html展示成功,中文正常
export LC_CTYPE=zh_CN.utf-8;echo "test body"|mailx -a ngxszqWchat.html -s testmail12 -v youmail@abc.com
# 文件以附件方式加入,同时有body内容,html展示成功,中文正常
export LC_CTYPE=zh_CN.utf-8;echo "test body有中文"|mailx -a ngxszqWchat.html -s testmail12 -v youmail@abc.com
--=_5d386fce.g5ugrxz+Ehh19s4kQAqN4QA/syPM/92w+c46HQwYezzhawAb
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit
Content-Disposition: inline
test body有中文
--=_5d386fce.g5ugrxz+Ehh19s4kQAqN4QA/syPM/92w+c46HQwYezzhawAb
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: 8bit
Content-Disposition: attachment; filename="ngxszqWchat.html"
...
# 无法改Content-Type,html被按源码展示
export LC_CTYPE=zh_CN.utf-8;mailx -s testmail11 -v youmail@abc.com
# cat -v添加内容,html被按源码展示
export LC_CTYPE=zh_CN.utf-8;cat -v ngxszqWchat.html|mailx -s testmail13 -v youmail@abc.com
# cat -v添加内容,html被按源码展示
export LC_CTYPE=zh_CN.utf-8;cat -v ngxszqWchat.html|mailx -s testmail13 -v youmail@abc.com
# 仍然无效
export LC_CTYPE=zh_CN.utf-8;(echo -e "
Content-Type: text/html; charset='utf-8'
Content-Transfer-Encoding: 8bit
Content-Disposition: inline
";cat -v ngxszqWchat.html)|mailx -s testmail13 -v youmail@abc.com
Sending mail from scripts
http://heirloom.sourceforge.net/mailx/mailx.1.html#12
If you want to send mail from scripts, you must be aware that mailx reads the user's configuration files by default. So unless your script is only intended for your own personal use (as e.g. a cron job), you need to circumvent this by invoking mailx like
MAILRC=/dev/null mailx -n
You then need to create a configuration for mailx for your script. This can be done by either pointing the MAILRC variable to a custom configuration file, or by passing the configuration in environment variables. Since many of the configuration options are not valid shell variables, the env command is useful in this situation. An invocation could thus look like
env MAILRC=/dev/null from=scriptreply@domain smtp=host \
smtp-auth-user=login smtp-auth-password=secret \
smtp-auth=login mailx -n -s "subject" \
-a attachment_file recipient@domain
MAILCMD="
env MAILRC=/dev/null LC_CTYPE=zh_CN.utf-8 charset=utf-8 send_charset='us-ascii:iso-8859-1:utf-8' \
from=myuser@test.com \
smtp=smtp.邮件服务器.com \
smtp-auth-user=myuser \
smtp-auth-password=mypass \
smtp-auth=login mailx -n "
echo "Test mail." | $MAILCMD -s 'test' youmail@test.com
mutt
Mutt 1.5.20 (2009-12-10)
usage: mutt [] [-z] [-f | -yZ]
mutt [] [-x] [-Hi ] [-s ] [-bc ] [-a [...] --] [...]
mutt [] [-x] [-s ] [-bc ] [-a [...] --] [...] < message
mutt [] -p
mutt [] -A [...]
mutt [] -Q [...]
mutt [] -D
mutt -v[v]
选项:
-A 扩展给出的别名
-a 附加一个文件到本信件作为附件
-b
指定一个密件抄送(BCC)地址-c
指定一个抄送(CC)地址-D 打印所有变量的值到标准输出
-e 指定一个初始化后要被执行的命令
-f 指定要阅读那一个信箱
-F 指定一个替代的 muttrc 文件
-H 指定一个模板文件以读取标题和正文来源
-i 指定一个 Mutt 需要包含在正文中的文件
-m 指定一个预设的信箱类型
-n 使 Mutt 不去读取系统的 Muttrc
-p 叫回一个延后寄送的信件
-Q 查询一个配置变量
-R 以只读模式打开信箱
-s 指定一个标题 (如果有空白的话必须被包括在引号中)
-v 显示版本和编译时的定义
-x 模拟 mailx 寄送模式
-y 选择一个被指定在您`mailboxes'清单中的信箱
-z 如果在信箱中没有信件的话,立即退出
-Z 打开第一个附有新信件的资料夹,如果没有的话立即离开
-h 本帮助消息
-- separate filename(s) and recipients,
when using -a, -- is mandatory
安装
yum install mutt -y
# 缺少这些包,会报"No authenticators available"错误:
yum install cyrus-sasl-gssapi cyrus-sasl-plain cyrus-sasl cyrus-sasl-lib -y
~/.muttrc
set imap_user = "yourName@gmail.com"
set imap_pass = "yourpassword"
# 这里也可以输入你的密码,但是为了安全,还是不输
set smtp_url = "smtp://YourName@smtp.gmail.com:587/"
set smtp_pass = "yourpassword"
# 这里也可以输入你的密码,但是为了安全,还是不输。
et smtp_authenticators="login"
set from = "YourName@gmail.com"
set realname = "yourname" #别人收到邮件,显示的名字
set folder = "imaps://imap.gmail.com:993"
set spoolfile = "+INBOX"
set postponed="+[Gmail]/Drafts"
set header_cache=~/.mutt/cache/headers
set message_cachedir=~/.mutt/cache/bodies
set certificate_file=~/.mutt/certificates
set move = no
set content_type="text/html"
set send_charset="us-ascii:iso-8859-1:utf-8"
set charset="utf-8"
set ssl_starttls=yes
set ssl_force_tls=yes
set ssl_verify_host=no
set ssl_verify_dates=no
发送邮件
echo 'test mutt'|mutt yourmail@abc.com -s 'test mutt'
(echo -e "
test mail
\n";cat /data/www/logs/csm_dcs_list.html) \
|mutt yourmail@abc.com -s 'test mutt'