我创建了一个脚本,每天晚上在我的Linux服务器上运行,它使用mysqldump将我的每个MySQL数据库备份到.sql文件,并将它们打包成一个压缩的.tar文件。我想完成的下一步是通过电子邮件将tar文件发送到远程电子邮件服务器以确保安全。我已经能够通过管道备份文本文件到mailx发送原始脚本正文的电子邮件,就像这样:

$ cat mysqldbbackup.sql | mailx backup@email.example

Cat回显备份文件的文本,该文本通过管道输入mailx程序,并将收件人的电子邮件地址作为参数传递。

虽然这实现了我所需要的,我认为它可以更好的一步,有任何方法,使用shell脚本或其他方式,将压缩的.tar文件作为附件发送到外发电子邮件消息?这将避免处理包含标题数据和经常有换行问题等非常长的电子邮件。


当前回答

另一种选择- Swaks (SMTP的瑞士军刀)。

swaks -tls \
    --to ${MAIL_TO} \
    --from ${MAIL_FROM} \
    --server ${MAIL_SERVER} \
    --auth LOGIN \
    --auth-user ${MAIL_USER} \
    --auth-password ${MAIL_PASSWORD} \
    --header "Subject: $MAIL_SUBJECT" \
    --header "Content-Type: text/html; charset=UTF-8" \
    --body "$MESSAGE" \
    --attach mysqldbbackup.sql

其他回答

或者,失败的杂种:

gzip -c mysqldbbackup.sql | uuencode mysqldbbackup.sql.gz  | mail -s "MySQL DB" backup@email.com

如果文件是文本,你可以在正文中最简单地发送它:

sendmail recipient@example.com < message.txt

发送带有mailx的明文正文电子邮件和一个明文附件:

(
  /usr/bin/uuencode attachfile.txt myattachedfilename.txt; 
  /usr/bin/echo "Body of text"
) | mailx -s 'Subject' youremail@example.com

下面是与上面相同的命令,没有换行符

( /usr/bin/uuencode /home/el/attachfile.txt myattachedfilename.txt; /usr/bin/echo "Body of text" ) | mailx -s 'Subject' youremail@example.com

确保你有一个文件/home/el/attachfile.txt,其中定义了以下内容:

<html><body>
Government discriminates against programmers with cruel/unusual 35 year prison
sentences for making the world's information free, while bankers that pilfer 
trillions in citizens assets through systematic inflation get the nod and 
walk free among us.
</body></html>

如果你没有uuencode,请阅读这个:https://unix.stackexchange.com/questions/16277/how-do-i-get-uuencode-to-work

在Linux上,用sendmail发送HTML正文电子邮件和PDF附件:

确保你已经安装了ksh: yum info ksh

确保已经安装和配置了sendmail。

确保已安装uuencode并可用:https://unix.stackexchange.com/questions/16277/how-do-i-get-uuencode-to-work

创建一个名为test.sh的新文件,并将其放在主目录:/home/el中

将以下代码放入test.sh:

#!/usr/bin/ksh
export MAILFROM="el@defiant.com"
export MAILTO="youremail@example.com"
export SUBJECT="Test PDF for Email"
export BODY="/home/el/email_body.htm"
export ATTACH="/home/el/pdf-test.pdf"
export MAILPART=`uuidgen` ## Generates Unique ID
export MAILPART_BODY=`uuidgen` ## Generates Unique ID

(
 echo "From: $MAILFROM"
 echo "To: $MAILTO"
 echo "Subject: $SUBJECT"
 echo "MIME-Version: 1.0"
 echo "Content-Type: multipart/mixed; boundary=\"$MAILPART\""
 echo ""
 echo "--$MAILPART"
 echo "Content-Type: multipart/alternative; boundary=\"$MAILPART_BODY\""
 echo ""
 echo "--$MAILPART_BODY"
 echo "Content-Type: text/plain; charset=ISO-8859-1"
 echo "You need to enable HTML option for email"
 echo "--$MAILPART_BODY"
 echo "Content-Type: text/html; charset=ISO-8859-1"
 echo "Content-Disposition: inline"
 cat $BODY
 echo "--$MAILPART_BODY--"

 echo "--$MAILPART"
 echo 'Content-Type: application/pdf; name="'$(basename $ATTACH)'"'
 echo "Content-Transfer-Encoding: uuencode"
 echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
 echo ""
 uuencode $ATTACH $(basename $ATTACH)
 echo "--$MAILPART--"
) | /usr/sbin/sendmail $MAILTO

更改test.sh顶部的导出变量,以反映您的地址和文件名。

下载一个测试pdf文档,并将其放在/home/el中,称为pdf-test.pdf

创建一个名为/home/el/email_body.htm的文件,并在其中放入以下一行:

<html><body><b>this is some bold text</b></body></html>

确保pdf文件有足够的755权限。

执行脚本。/test.sh

Check your email inbox, the text should be in HTML format and the pdf file automatically interpreted as a binary file. Take care not to use this function more than say 15 times in a day, even if you send the emails to yourself, spam filters in gmail can blacklist a domain spewing emails without giving you an option to let them through. And you'll find this no longer works, or it only lets through the attachment, or the email doesn't come through at all. If you have to do a lot of testing on this, spread them out over days or you'll be labelled a spammer and this function won't work any more.

使用mailx命令

 echo "Message Body Here" | mailx -s "Subject Here" -a file_name user@example.com

使用sendmail

#!/bin/ksh

fileToAttach=data.txt

`(echo "To: user@company.example"
  echo "Cc: user@company.example"
  echo "From: Application"
  echo "Subject: your subject"
  echo  your body
  uuencode $fileToAttach $fileToAttach
  )| eval /usr/sbin/sendmail -t `;

根据您的Linux版本,它可能被称为邮件。引用上文@David的话:

mail -s "Backup" -a mysqldbbackup.sql backup@email.example < message.txt

或者:

cat message.txt | mail -s "Backup" -a mysqldbbackup.sql backup@email.example