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

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

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

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


当前回答

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

sendmail recipient@example.com < message.txt

其他回答

只是补充我的2分,我会写我自己的PHP脚本:

http://php.net/manual/en/function.mail.php

在该页上的示例中,有很多方法来处理附件。

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

sendmail recipient@example.com < message.txt

或者,失败的杂种:

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

不是一种发送电子邮件的方法,但你可以使用在线Git服务器(例如Bitbucket或类似的服务)。

通过这种方式,你可以使用git push命令,所有版本都将以压缩和有组织的方式存储。

发送带有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.