mailx - 邮件发送
mailx
#!/bin/sh
SUBJECT='test mail'
RECIPIENT='user@domain.com'
FILE='TEST.html'
cat - $FILE <<END | /usr/sbin/sendmail -i $RECIPIENT
MIME-Version: 1.0
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 8bit
Subject: $SUBJECT
To: $RECIPIENT
END
# 另还可用 -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"
<html>
<head>
...
# 无法改Content-Type,html被按源码展示
export LC_CTYPE=zh_CN.utf-8;mailx -s testmail11 -v youmail@abc.com <ngxszqWchat.html
# 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 <content_file
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 [<options>] [-z] [-f <file> | -yZ]
mutt [<options>] [-x] [-Hi <file>] [-s <subj>] [-bc <addr>] [-a <file> [...] --] <addr> [...]
mutt [<options>] [-x] [-s <subj>] [-bc <addr>] [-a <file> [...] --] <addr> [...] < message
mutt [<options>] -p
mutt [<options>] -A <alias> [...]
mutt [<options>] -Q <query> [...]
mutt [<options>] -D
mutt -v[v]
选项:
-A <alias> 扩展给出的别名
-a <file> 附加一个文件到本信件作为附件
-b <address> 指定一个密件抄送(BCC)地址
-c <address> 指定一个抄送(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