How do I send a file as an email attachment using

2018-12-31 10:35发布

I've created a script that runs every night on my Linux server that uses mysqldump to back up each of my MySQL databases to .sql files and packages them together as a compressed .tar file. The next step I want to accomplish is to send that tar file through email to a remote email server for safekeeping. I've been able to send the raw script in the body an email by piping the backup text file to mailx like so:

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

cat echoes the backup file's text which is piped into the mailx program with the recipient's email address passed as an argument.

While this accomplishes what I need, I think it could be one step better, Is there any way, using shell scripts or otherwise, to send the compressed .tar file to an outgoing email message as an attachment? This would beat having to deal with very long email messages which contain header data and often have word-wrapping issues etc.

25条回答
孤独总比滥情好
2楼-- · 2018-12-31 11:04

From looking at man mailx, the mailx program does not have an option for attaching a file. You could use another program such as mutt.

echo "This is the message body" | mutt -a file.to.attach -s "subject of message" recipient@domain.com

Command line options for mutt can be shown with mutt -h.

查看更多
泛滥B
3楼-- · 2018-12-31 11:05

From source machine

mysqldump --defaults-extra-file=sql.cnf database | gzip | base64 | mail me@myemail.com

On Destination machine. Save the received mail body as db.sql.gz.b64; then..

base64 -D -i db.sql.gz.b64 | gzip -d | mysql --defaults-extra-file=sql.cnf
查看更多
忆尘夕之涩
4楼-- · 2018-12-31 11:06

Or, failing mutt:

gzip -c mysqldbbackup.sql | uuencode mysqldbbackup.sql.gz  | mail -s "MySQL DB" backup@email.com
查看更多
伤终究还是伤i
5楼-- · 2018-12-31 11:06

One more thing about mutt: by default it uses your address and name in "From:" field. If it's not what you need, you can create alternative muttrc file containing a string like this: set from="My mail daemon "

Use this file with -F command line option.

查看更多
回忆,回不去的记忆
6楼-- · 2018-12-31 11:07

You can use mutt to send the email with attachment

mutt -s "Backup" -a mysqldbbackup.sql backup@email.com < message.txt
查看更多
伤终究还是伤i
7楼-- · 2018-12-31 11:09

I use SendEmail, which was created for this scenario. It's packaged for Ubuntu so I assume it's available

sendemail -f sender@some.where -t receiver@some.place -m "Here are your files!" -a file1.jpg file2.zip

http://caspian.dotconf.net/menu/Software/SendEmail/

查看更多
登录 后发表回答