linux - [Solved-5 Solutions] How to send a file as an email attachment using Linux command line - ubuntu - red hat - debian - linux server - linux pc



Linux - Problem :

$ cat mysqldbbackup.sql | mailx backup@email.com
click below button to copy the code. By - Linux tutorial - team

How to send a file as an email attachment using Linux command line ?

Linux - Solution 1:

echo "This is the message body" | mutt -a "/path/to/file.to.attach" -s "subject of message" -- recipient@domain.com
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 2:

gzip -c mysqldbbackup.sql | uuencode mysqldbbackup.sql.gz  | mail -s "MySQL DB" backup@email.com
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 3:

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
click below button to copy the code. By - Linux tutorial - team

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

Linux - Solution 4:

Depending on your version of linux it may be called mail.

mail -s "Backup" -a mysqldbbackup.sql backup@email.com < message.txt
click below button to copy the code. By - Linux tutorial - team

or also:

cat message.txt | mail -s "Backup" -a mysqldbbackup.sql backup@email.com 
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 5:

mpack -s subject file user@example.com
click below button to copy the code. By - Linux tutorial - team

Unfortunately mpack does not recognize '-' as an alias for stdin. But the following work, and can easily be wrapped in an (shell) alias or a script:

mpack -s subject /dev/stdin user@example.com < file
click below button to copy the code. By - Linux tutorial - team

Related Searches to - linux - linux tutorial - How to send a file as an email attachment using Linux command line