Send attachment in Linux


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Send attachment in Linux
# 1  
Old 12-02-2008
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.

Last edited by shamushamu; 12-02-2008 at 09:29 PM..
# 2  
Old 12-02-2008
there are dozens of threads covering this, use the search, there are many different ways to do it.
# 3  
Old 12-03-2008
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
# 4  
Old 12-03-2008
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
# 5  
Old 12-03-2008
this will give you something nice

Code:
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.

Last edited by unclecameron; 12-03-2008 at 03:33 PM..
# 6  
Old 12-04-2008
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
# 7  
Old 12-04-2008
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 Smilie

This thread is closed.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Not able to send attachment with the email

Hi All, The below code is working fine for me. mailx -s hello abc@xyz.com <<EOT Hello !!! How are you? Regards Rahul EOT But i am not able to send csv file with the mail .Getting just themail but not the attachment. uuencode /path/s1.csv | mailx -s hello abc@xyz.com <<EOT... (9 Replies)
Discussion started by: rafa_fed2
9 Replies

2. Shell Programming and Scripting

Send mail as attachment

Hi All, I am trying to send mail via unix and attaching file along with that mail, but facing issue while sending. I have tried below commands- 1)- uuencode $prmDirOutput/report_mailbody $prmDirOutput/report_mailbody | mailx -s "Acknowledgment file- $subject" $email_id 2)- mutt -a... (4 Replies)
Discussion started by: Amit786
4 Replies

3. Shell Programming and Scripting

Send mail using attachment

Hi All, I need to send mail with attachment. I am using uuencode but it seems it is sending encoded file. I am getting the mail with attachment but there is no data even the file size is almost 90KB. Please help. Thanks. (3 Replies)
Discussion started by: unankix
3 Replies

4. Shell Programming and Scripting

How to send attachment without using uuencode

H All I want to send attachment in mail but I dont have uuencode installed in AIX server, there is any alternative way to send attachment in mail. (2 Replies)
Discussion started by: ns64110
2 Replies

5. UNIX for Dummies Questions & Answers

How to send email attachment as read only in Linux

Hi, Could anyone help me to find soultion for the issue to send email attachment as read only. I can change the file permission in Linux and can send the email attachment from Linux. But my requirment is user can read the attachment but should not modified it . even user save the attachment... (4 Replies)
Discussion started by: calagar
4 Replies

6. Shell Programming and Scripting

How to send a mail with attachment?

Hi, I usually write a file TEST.MAIL like this to send mails: Importance: High Priority: Urgent X-Priority: 1 (Highest) From: user Subject: error ... text body .... and then I launch it (or writre a c-chell that launchs it) by writing: mail a@b.com < /users/.../TEST.MAIL How can... (4 Replies)
Discussion started by: albaalbetti
4 Replies

7. Shell Programming and Scripting

How to send a attachment

Hi, I want to send a attachment using mailx command and also i want to include body of the mail which is stored in a text file.:confused: uuencode att.txt att.txt |mailx -s "attachment" adc@mail.com < text.txt I tried this command, but it is not working fine. It is showing the body but it is... (2 Replies)
Discussion started by: lathish
2 Replies

8. UNIX for Advanced & Expert Users

send attachment without uuencode

Hello - In unix, can you tell me IF there is a way to send attachments via email without using uuencode command? Thank you (3 Replies)
Discussion started by: panchpan
3 Replies

9. UNIX for Dummies Questions & Answers

Send an attachment with metasend

Hi, How to send an attachment of which the name contains the space. Following is the command I tried. metasend -b -s "test subject" -F "test@yahoo.com" -t "test@test.com" -f "test.txt" -m "text/plain" -n -f "test 123.txt" -m "application/xls" If the file name contains no space,... (0 Replies)
Discussion started by: egckhad
0 Replies
Login or Register to Ask a Question