by Ramesh Natarajan on December 30, 2009
Question: How do I send an email with attachment from Linux command-line
(or shell script)? Also, can I send both attachment and body text together
in an email from Linux command-line?
Answer: You can send both attachment and body text (or just the attachment
with a subject line) from Linux command line as explained below.
1. Send an Email with Subject and Body
Typically you would send an email from the Linux command line with a
subject line and body text as shown below. Please note that you should
type a . (period) in a separate line to indicate the body of the text is
over.
$ mail ramesh.thegeekstuff@gmail.com
Subject: Email Testing from Linux
Dear,
It is very easy to send an email from Linux command line.
Thanks, Ramesh
.
Cc: ramesh@thegeekstuff.com
If you want to read the body text from a file (for example,
body-message.txt), send the email as shown below.
$ cat body-message.txt | mail -s "Email testing from Linux"
ramesh@thegeekstuff.com
2. Send an Email with Attachment
To send an attachment from the email, use uuencode command. On RedHat (and
related distributions), uuencode is part of the sharutils package. So,
install the sharutils as shown below.
# rpm -ivh sharutils-4.6.1-2.i386.rpm
Preparing... ############################## [100%]
1:sharutils ############################## [100%]
Once you've confirmed that you have uuencode, send the email with an
attachment as shown below.
$ uuencode input-attachment.txt output-attachment.txt | \
mail -s "Email With Attachment" ramesh.thegeekstuff@gmail.com
In this example,
input-attachment.txt is the file that you like to attach to the email.
If you like the file to be attached with a different name, specify it as
2nd parameter to the uuencode. In this example, the intput-attachment.txt
file content will be attached as output-attachment.txt
Note: uuencode can also be used to send base64 attachments as shown below.
$ uuencode -m input-attachment.txt output-attachment.txt | \
mail -s "Email With Base64 Attachment" ramesh.thegeekstuff@gmail.com
3. Send an Email with Attachment and Body
You can send an email with both attachment and body message as shown below.
$ ( cat body-message.txt; uuencode input-attachment.txt
output-attachment.txt ) \
| mail -s "Email With Body Text and Attachment"
ramesh.thegeekstuff@gmail.com
没有评论:
发表评论