mailx、mutt和swaks。我们即将呈现的这些工具都是非常有名的,并且存在于多数Linux发行版默认的软件仓库中,你可以使用如下命令安装:在 Debian / Ubuntu 系统apt-get install muttapt-get install swaksapt-get install mailxapt-get install sharutilsyum install muttyum install swaksyum install mailxyum install sharutilsmailx工具在多数Linux发行版中是默认的邮件程序,现在已经支持发送附件了。如果它不在你的系统中,你可以使用上边的命令安装。有一点需要注意,老版本的mailx可能不支持发送附件,运行如下命令查看是否支持。$ man mailmailx [-BDdEFintv~][-s subject][-a attachment ][-c cc-addr][-b bcc-addr][-r from-addr][-h hops][-A account][-S variable[=value]] to-addr ...-a的选项(-a 文件名,将文件作为附件添加到邮件)和-s选项(-s 主题,指定邮件的主题),那就是支持的。可以使用如下的几个例子发送邮件。a) 简单的邮件运行mail命令,然后mailx会等待你输入邮件内容。你可以按回车来换行。当输入完成后,按Ctrl + D,mailx会显示EOT表示结束。然后mailx会自动将邮件发送给收件人。$ mail user@example.comHI,GoodMorningHow are youEOT$ echo "Email text"| mail -s "Test Subject" user@example.com-s的用处是指定邮件的主题。c) 从文件中读取邮件内容并发送$ mail -s "message send from file" user@example.com </path/to/fileecho命令输出作为邮件内容发送$ echo "This is message body"| mail -s "This is Subject" user@example.com$ echo “Bodywith attachment "| mail -a foo.tar.gz -s "attached file" user@example.com-a选项用于指定附件。 $ mutt -s "Testing from mutt" user@example.com </tmp/message.txtecho命令输出作为邮件内容发送$ echo "This is the body"| mutt -s "Testing mutt" user@example.com$ echo "This is the body"| mutt -s "Testing mutt" user@example.com -a /tmp/foo.tar.gz$ echo "This is the body"| mutt -s "Testing" user@example.com -a foo.tar.gz –a bar.tar.gz$ swaks -t "foo@bar.com"--header "Subject: Subject"--body "Email Text"--attach foo.tar.gzuuencode(“UNIX to UNIX encoding”,UNIX之间使用的编码方式)程序用来解决这???限制。使用uuencode,发送端将二进制格式的转换成文本格式来传输,接收端再转换回去。我们可以简单地使用uuencode和mailx或者mutt配合,来发送二进制内容,类似这样:$ uuencode example.jpeg example.jpeg | mail user@example.com#!/bin/bashFROM=""SUBJECT=""ATTACHMENTS=""TO=""BODY=""# 检查文件名对应的文件是否存在function check_files(){output_files=""for file in $1doif[-s $file ]thenoutput_files="${output_files}${file} "fidoneecho $output_files}echo "*********************"echo "E-mail sending script."echo "*********************"echo# 读取用户输入的邮件地址while[1]doif[! $FROM ]thenecho -n -e "Enter the e-mail address you wish to send mail from:
[Enter] "elseecho -n -e "The address you provided is not valid:
[Enter] "firead FROMecho $FROM | grep -E "^.+@.+$">/dev/nullif[ $?-eq 0]thenbreakfidoneecho# 读取用户输入的收件人地址while[1]doif[! $TO ]thenecho -n -e "Enter the e-mail address you wish to send mail to:
[Enter] "elseecho -n -e "The address you provided is not valid:
[Enter] "firead TOecho $TO | grep -E "^.+@.+$">/dev/nullif[ $?-eq 0]thenbreakfidoneecho# 读取用户输入的邮件主题echo -n -e "Enter e-mail subject:
[Enter] "read SUBJECTechoif["$SUBJECT"==""]thenecho "Proceeding without the subject..."fi# 读取作为附件的文件名echo -e "Provide the list of attachments. Separate names by space.If there are spaces in file name, quote file name with "."read attecho# 确保文件名指向真实文件attachments=$(check_files "$att")echo "Attachments: $attachments"for attachment in $attachmentsdoATTACHMENTS="$ATTACHMENTS-a $attachment "doneecho# 读取完整的邮件正文echo "Enter message. To mark the end of message type ;; in new line."read linewhile["$line"!=";;"]doBODY="$BODY$line
"read linedoneSENDMAILCMD="mutt -e "set from=$FROM" -s "$SUBJECT" $ATTACHMENTS -- "$TO" <<< "$BODY""echo $SENDMAILCMDmutt -e "set from=$FROM"-s "$SUBJECT" $ATTACHMENTS -- $TO <<< $BODY$ bash send_mail.sh*********************E-mail sending script.*********************Enter the e-mail address you wish to send mail from:[Enter] test@gmail.comEnter the e-mail address you wish to send mail to:[Enter] test@gmail.comEnter e-mail subject:[Enter]Message subjectProvide the list of attachments.Separate names by space.If there are spaces in file name, quote file name with".send_mail.shAttachments: send_mail.shEnter message. To mark the end of message type ;; in new line.This is a messagetext;;