Send attachment in Linux

Hi, I have searched the site for info on sending an attachment in Linux, have tried a few people mailx codes but to no success. Can someone please help me? This is what i have so far:

#!/bin/bash

user_mail_address="me@yahoo.com"
filename="attachment.txt"
body="This is the body"

mail -s "This is the subject" $user_mail_address $filename

How do i modify the code, so that I can add a file attachment.txt and also type something in the body of my email? Thanks.

there are dozens of threads covering this, use the search, there are many different ways to do it.

Yes, I have tried a few of them. They used mailx instead of mail. Also, once I ran their scripts, its waiting for me to input something. So I typed in a letter or a filename, but it still continue to wait for an input. I had to press control C to exit. Then it says --interrupt once more to kill.

I will continue to search, but so far no luck yet after looking at many threads. Here are a couple of codes from different folks that doesn't work for me. It just wait for me to input something in the terminal:

CODE#1
( cat a
uuencode b.txt
) | mail -s "Attachment Sent" $mail_user

CODE#2
(echo "`cat a`"; uuencode t /home/lang/script/b.txt) | mail -s "Attachment Sent" $mail_user

the command to send a file as body of the e-mail is
#mail user@e-mail.com -s 'hosts file' < /etc/hosts
this will show the file etc hosts as the contents of the email and hosts file in the subject line.
try something along these lines
#mail user@e-mail.com -s "Attachment Sent" < /home/lang/script/b.txt

this will give you something nice

awk 'BEGIN{print "Subject:Bash rocks!\nFrom:Whoever <your@email.com>"}{printf("%s\015\n", $0)}' filecontentsforbody.txt | sendmail -t your@email.com

but this will only give you a nice email with filecontentsforbody.txt as the email body. I don't know how to attach somefile.zip or whatever to your email.

read uuencode man page

$> (echo "Mail body"; uuencode attachment.file attachment.file ) | mailx -s "subject" to_address

Note the use of uuencode
uuencode attachment.file attachment.file

Thanks to everyone who replied. What happened to my Linux is my root user told me that I need a reboot. After the reboot, then my email with attachment works. Here's my code with a subject, body, and an attachment:

#!/bin/bash

FILE=$1
RECIPIENT=myemail@yahoo.com

(echo "This file $FILE was sent from Linux."
for i in `ls -1 $FILE`
do
file_name=`basename $i`
uuencode $i $file_name
done
) | mail -s "Mail sent with the attachment file: $FILE" "$RECIPIENT"

To use this script, just type in the name of the script followed by the filename. I thought I'd share this because I had a hard time getting to this point :slight_smile:

This thread is closed.