如何在linux終端發電子郵件(帶附件)

來源:互聯網
上載者:User
How To Send an Email With Attachment and Body from Linuxnn

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.