复制代码代码如下:$ uuencode example.jpeg example.jpeg | mail user@example.com Shell脚本:解释如何发送邮件
复制代码代码如下: #!/bin/bash FROM="" SUBJECT="" ATTACHMENTS="" TO="" BODY="" # 检查文件名对应的文件是否存在 function check_files() { output_files="" for file in $1 do if [ -s $file ] then output_files="${output_files}${file} " fi done echo $output_files } echo "*********************" echo "E-mail sending script." echo "*********************" echo # 读取用户输入的邮件地址 while [ 1 ] do if [ ! $FROM ] then echo -n -e "Enter the e-mail address you wish to send mail from:
[Enter] " else echo -n -e "The address you provided is not valid:
[Enter] " fi read FROM echo $FROM | grep -E "^.+@.+$" > /dev/null if [ $? -eq 0 ] then break fi done echo # 读取用户输入的收件人地址 while [ 1 ] do if [ ! $TO ] then echo -n -e "Enter the e-mail address you wish to send mail to:
[Enter] " else echo -n -e "The address you provided is not valid:
[Enter] " fi read TO echo $TO | grep -E "^.+@.+$" > /dev/null if [ $? -eq 0 ] then break fi done echo # 读取用户输入的邮件主题 echo -n -e "Enter e-mail subject:
[Enter] " read SUBJECT echo if [ "$SUBJECT" == "" ] then echo "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 att echo # 确保文件名指向真实文件 attachments=$(check_files "$att") echo "Attachments: $attachments" for attachment in $attachments do ATTACHMENTS="$ATTACHMENTS-a $attachment " done echo # 读取完整的邮件正文 echo "Enter message. To mark the end of message type ;; in new line." read line while [ "$line" != ";;" ] do BODY="$BODY$line
" read line done SENDMAILCMD="mutt -e "set from=$FROM" -s "$SUBJECT" $ATTACHMENTS -- "$TO" <<< "$BODY"" echo $SENDMAILCMD mutt -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.com Enter the e-mail address you wish to send mail to: [Enter] test@gmail.com Enter e-mail subject: [Enter] Message subject Provide the list of attachments. Separate names by space. If there are spaces in file name, quote file name with ". send_mail.sh Attachments: send_mail.sh Enter message. To mark the end of message type ;; in new line. This is a message text ;; 总结 有很多方法可以使用命令行/Shell脚本来发送邮件,这里我们只分享了其中4个类Unix系统可用的工具。希望你喜欢我们的文章,并且提供您的宝贵意见,让我们知道您想了解哪些新工具。