Sending email with text & attachment using mailx


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sending email with text & attachment using mailx
# 1  
Old 04-26-2005
Sending email with text & attachment using mailx

I spent some time working this out, with a little help from various forums, and thought the community would like to know :

Here is how you can send an email from a single Unix command line :

/usr/bin/echo "Email text\nNew line\nAnother new line" >x | uuencode sourcefile.txt sourcefile.txt | cat x - | mailx -s "Email subject" -r "reply@to.com" "recipient@company.com"

Breakdown :
- /usr/bin/echo : I use /usr/bin/echo because it consistently handles embedded control characters like \n (newline)

- >x : The echoed text is saved in file "x" for use by "cat"

- uuencode : This takes the file and generates it as a MIME-encoded attachment. The output (encoded file) is piped to "cat"

- cat : Concatenates the echoed text to the encoded file. Cat takes the contents of file "x", appends the output from uuencode and pipes them to "mailx"

- mailx : Sends the email. -s is the subject text, -r is the reply-to email address, followed by the recipient's email address. The contents of the email come from the output of "cat"

This command does leave behind a file called "x". You may want to delete this file later. You could save it in /var/tmp, if the contents aren't sensitive.

This works for me on Solaris (SunOS 5.6). I believe it should be fairly portable to other environments.

If you want to include HTML in the text part of the email, you can include "Content-Type: text/html" in the echoed text, followed by your HTML code.
# 2  
Old 04-26-2005
Excelent:-)

I try it ok in FreeBSD:-)
1.)/usr/bin/echo "Email text\nNew line\nAnother new line" >x

2.) uuencode sourcefile.txt sourcefile.txt | cat x - | mailx -s "Email subject" "recipient@company.com"

Thanks a lot;
Before you post,I can do only post content or post attechment with command line;can't bind them :-(
cat content.txt | mailx -s "Email subject" "recipient@company.com"
or
cat attechment.tar.gz | uuencode attechmetn.tar.gz | mailx -s "Email subject"
"recipient@company.com"
# 3  
Old 04-27-2005
You could use a sub-shell, e.g....
Code:
( echo "Email text"
  echo "etc"
  uuencode /path/to/sourcefile.txt sourcefile.txt
) | mailx -s "Email subject" "recipient@company.com"

# 4  
Old 07-27-2006
I tried this command but i m not getting the mail!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need help sending attachment via mailx

Hello, I am new to the Unix thing, and I am having trouble sending attachments via shell client putty through mailx. The command I use is $ mailx -s "Subject" user@blah.com < attachment.txt but everytime I do that it would say Cannot open attachment.txt I have the file save to my computer... (5 Replies)
Discussion started by: mrobin20
5 Replies

2. Shell Programming and Scripting

mailx not sending excel attachment properly

Hi, I am trying to send email with attachment using mailx command. I am using the folowing command: uuencode XX_HWSW_BUYERWISE_88963631_1.xls XX_HWSW_BUYERWISE_88963631_1.xls | mailx -s "Test Mail as Attachment" oracleams@xyz.com I get the email in the inbox. However, when I try to open the... (5 Replies)
Discussion started by: asp_julius
5 Replies

3. Shell Programming and Scripting

sending an email with attachment

Hi, Please help me in sending an e-mail with attachment through unix mailx command is not present in our unix box. (4 Replies)
Discussion started by: dudd9
4 Replies

4. Shell Programming and Scripting

Not able to attach text in body of email while sending mail with attachment

Hi, We have been trying to send mail with attachment and it is going fine, but when we try to attach a text to the body of the email, we find that the mail is going fine with the body text but the attachment is not going through. We are using ksh. The command that is successfull without the... (6 Replies)
Discussion started by: jmathew99
6 Replies

5. Shell Programming and Scripting

Sending Email with Attachment

Hi, I want to send an email with multiple attachment using uuencode and mail command. I am able to send with one attachment Ex:uuencode abc.txt abc.txt | mail test@test.com -s "Test3" Can anyone reply with syntax. Regards BS (3 Replies)
Discussion started by: balajiora
3 Replies

6. Shell Programming and Scripting

sending an attachment through email

As a part of requirement I need to send out mails with attachment from UNIX. I have to take query the Oracle DB and send the result of the query in an attachment through mail. I use the following script for the same. #!/bin/csh #!/bin/bash #!/bin/ksh ATTFILE=/folder1/test.xls cd... (1 Reply)
Discussion started by: Sgiri1
1 Replies

7. Shell Programming and Scripting

mailx attachment sending large file

Hi I want to sent attachment file which is 400mb size.(single file, not tar file) is there any way that these kind of large files can be divided into small sizes and sent as attachments thanks with anticipation (3 Replies)
Discussion started by: karthikn7974
3 Replies

8. Shell Programming and Scripting

Problem while sending message and attachment with mailx

Hi All, I am trying to send a mail with an attachment and message. Following command I am using. (cat <messagefile> ; uuencode <attachmentfile> <attachmentfile>)|mailx -s"Subject" dave@email.com In the received mail, message body is appearing fine. But attachment is not coming. Rather... (2 Replies)
Discussion started by: nihar.dutta
2 Replies

9. Shell Programming and Scripting

Problem with mailx command when sending attachment.

Hi, I have tried to sent a mail with body and attachment. But the shell script got hanging while executing that command. The command is "(cat body;uuencode att1.csv)|mailx -s "Production Monitoring Report(Unix Side)" milton.yesusundaram@patni.com" where body is a file having a single line.... (2 Replies)
Discussion started by: miltony
2 Replies

10. UNIX for Advanced & Expert Users

email attachment, with a message using mailx

Hi, I am trying to include a message along with an attachment with an email using mailx on AIX. uuencode Test.dat Test.dat| mailx -s 'Testing' mymail@yahoo.com < MESGFILE This only gives me the contents of MESGFILE as my message. If I remove the < FILE I recieve the attachment. What... (4 Replies)
Discussion started by: edog
4 Replies
Login or Register to Ask a Question