Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the commands to send e-mail under Linux

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains what are the commands to send e-mail under Linux. Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what are the commands for sending email under Linux.

In today's article, we will talk about some ways to use Linux command-line tools to send email with attachments. It has many uses, such as sending a file by e-mail on the server where the application is located, or you can use these commands in scripts to do some automation. In the example in this article, we will use the foo.tar.gz file as an attachment.

There are different command-line tools to send emails, and here I share a few tools that most users will use, such as mailx, mutt, and swaks.

The tools we will present are very famous and exist in the default software repositories of most Linux distributions, which you can install with the following command:

In Debian / Ubuntu system

The code is as follows:

Apt-get install mutt

Apt-get install swaks

Apt-get install mailx

Apt-get install sharutils

On Red Hat-based systems such as CentOS or Fedora

The code is as follows:

Yum install mutt

Yum install swaks

Yum install mailx

Yum install sharutils

1) use mail / mailx

The mailx tool is the default mail program in most Linux distributions and now supports sending attachments. If it is not on your system, you can use the command above to install it. It is important to note that older versions of mailx may not support sending attachments. Run the following command to see if it supports it.

The code is as follows:

$man mail

The first line looks like this:

The code is as follows:

Mailx [- BDdEFintv~] [- s subject] [- an attachment] [- c cc-addr] [- b bcc-addr] [- r from-addr] [- h hops] [- An account] [- S variable [= value]] to-addr. . .

If you see that it supports the option of-a (- a file name, add the file as an attachment) and the-s option (- s subject, specify the subject of the message), it is supported. You can use the following examples to send an email.

A) simple mail

Run the mail command, and mailx will wait for you to enter the contents of the email. You can press enter to change the line. When the input is complete, pressing Ctrl + DMailMailx will display EOT to indicate the end.

Mailx then automatically sends the message to the recipient.

The code is as follows:

$mail user@example.com

HI

Good Morning

How are you

EOT

B) send messages with a subject

The code is as follows:

$echo "Email text" | mail-s "Test Subject" user@example.com

The use of-s is to specify the subject of the message.

C) read the message from the file and send

The code is as follows:

$mail-s "message send from file" user@example.com

< /path/to/file d) 将从管道获取到的echo命令输出作为邮件内容发送 代码如下: $ echo "This is message body" | mail -s "This is Subject" user@example.com e) 发送带附件的邮件 代码如下: $ echo "Body with attachment "| mail -a foo.tar.gz -s "attached file" user@example.com -a选项用于指定附件。 2) mutt Mutt是类Unix系统上的一个文本界面邮件客户端。它有20多年的历史,在Linux历史中也是一个很重要的部分,它是最早支持进程打分和多线程处理的客户端程序之一。按照如下的例子来发送邮件。 a) 带有主题,从文件中读取邮件的正文,并发送 代码如下: $ mutt -s "Testing from mutt" user@example.com < /tmp/message.txt b) 通过管道获取echo命令输出作为邮件内容发送 代码如下: $ echo "This is the body" | mutt -s "Testing mutt" user@example.com c) 发送带附件的邮件 代码如下: $ echo "This is the body" | mutt -s "Testing mutt" user@example.com -a /tmp/foo.tar.gz d) 发送带有多个附件的邮件 代码如下: $ echo "This is the body" | mutt -s "Testing" user@example.com -a foo.tar.gz –a bar.tar.gz 3) swaks Swaks(Swiss Army Knife,瑞士军刀)是SMTP服务上的瑞士军刀,它是一个功能强大、灵活、可编程、面向事务的SMTP测试工具,由John Jetmore开发和维护。你可以使用如下语法发送带附件的邮件: 代码如下: $ swaks -t "foo@bar.com" --header "Subject: Subject" --body "Email Text" --attach foo.tar.gz 关于Swaks一个重要的地方是,它会为你显示整个邮件发送过程,所以如果你想调试邮件发送过程,它是一个非常有用的工具。 它会给你提供了邮件发送过程的所有细节,包括邮件接收服务器的功能支持、两个服务器之间的每一步交互。 (LCTT 译注:原文此处少了 sharutils 的相关介绍,而多了 uuencode 的介绍。) 4) uuencode 邮件传输系统最初是被设计来传送7位编码(类似ASCII)的内容的。这就意味这它是用来发送文本内容,而不能发会使用8位的二进制内容(如程序文件或者图片)。uuencode("UNIX to UNIX encoding",UNIX之间使用的编码方式)程序用来解决这个限制。使用uuencode,发送端将二进制格式的转换成文本格式来传输,接收端再转换回去。 我们可以简单地使用uuencode和mailx或者mutt配合,来发送二进制内容,类似这样: 代码如下: $ uuencode example.jpeg example.jpeg | mail user@example.com Shell脚本:解释如何发送邮件 代码如下: #!/bin/bash FROM="" SUBJECT="" ATTACHMENTS="" TO="" BODY="" # 检查文件名对应的文件是否存在 function check_files() { output_files="" for file in $1 do if [ -s $file ] then output_files="${output_files}${file} " fi done echo $output_files } echo "*********************" echo "E-mail sending script." echo "*********************" echo # 读取用户输入的邮件地址 while [ 1 ] do if [ ! $FROM ] then echo -n -e "Enter the e-mail address you wish to send mail from:\n[Enter] " else echo -n -e "The address you provided is not valid:\n[Enter] " fi read FROM echo $FROM | grep -E '^.+@.+$' >

/ dev/null

If [$?-eq 0]

Then

Break

Fi

Done

Echo

# read the recipient address entered by the user

While [1]

Do

If [! $TO]

Then

Echo-n-e "Enter the e-mail address you wish to send mail to:\ n [enter]"

Else

Echo-n-e "The address you provided is not valid:\ n [enter]"

Fi

Read TO

Echo $TO | grep-E'^. + @. + $'> / dev/null

If [$?-eq 0]

Then

Break

Fi

Done

Echo

# read the email subject entered by the user

Echo-n-e "Enter e-mail subject:\ n [enter]"

Read SUBJECT

Echo

If ["$SUBJECT" = ""]

Then

Echo "Proceeding without the subject..."

Fi

# read the file name as an attachment

Echo-e "Provide the list of attachments. Separate names by space.

If there are spaces in file name, quote file name with\ "."

Read att

Echo

# make sure the file name points to the real file

Attachments=$ (check_files "$att")

Echo "Attachments: $attachments"

For attachment in $attachments

Do

ATTACHMENTS= "$ATTACHMENTS-a $attachment"

Done

Echo

# read the full email body

Echo "Enter message. To mark the end of message type;; in new line."

Read line

While ["$line"! = ";;]

Do

BODY= "$BODY$line\ n"

Read line

Done

SENDMAILCMD= "mutt-e\" set from=$FROM\ "- s\" $SUBJECT\ "\

$ATTACHMENTS--\ "$TO\"

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report