Shell Script

Chapter 20 Using E-Mail

트리스탄1234 2023. 6. 21. 01:03
728x90
반응형

Nowadays, most of our daily life is done through e-mail, so it is necessary to use e-mail in Linux as well. So, in this chapter, we will learn how to use e-mail in Linux. ​

The Linux system is derived from the Unix environment. The figure below shows the structure of the e-mail environment in Linux.

The e-mail system in Linux was used to implement the e-mail system used in the old Unix and was moved to the Linux environment. ​

There are three main functions in e-mail as follows.

■ The Mail Transfer Agent (MTA)

■ The Mail Delivery Agent (MDA)

■ The Mail User Agent (MUA)

The Mail Transfer Agent

The MTA is the core sw in the e-mail system. If the recipient of the mail is local, that is, on the same server, the MTA delivers the message to the local mailbox or the local MDA. When sending to a remote mail server instead of local, you need to create a communication link with the mta of the remote server. There are two methods for this.

■ Direct delivery

■ Proxy delivery

In case of direct connection to the Internet, the message is delivered directly. In this case, the MTA uses DNS to find out the IP address of the destination, then creates a TCP session using the SMTP and delivers it. ​ However, these days, for security reasons, indirect delivery using a proxy server is used rather than direct delivery. ​

Among MTA software, popular programs are sendmail and Postfix.

The Mail Delivery Agent

The MDA program is to deliver messages that are intended for local users. It receives messages from the MTA and decides exactly where and how to deliver them. ​ The figure below is an MDA diagram.

There are three types of mail storage that MDA delivers messages to.

■ /var/spool/mail or /var/mail files

■ $HOME/mail files

■ Maildir-style mailbox directories

Most Linux distributions use the /var/spool/mail or /var/mail directory, with one file for each user.

The Mail User Agent

So far, we have tracked email traffic from remote hosts to local hosts and to individuals. User's mailbox. The next step in the process is to allow individual users to view their e-mail. ​ The MUA program provides users with a way to read messages in their own mailboxes. ​ Among the various open sources, the most popular is Malix. Below is an example of how to use Malix. .

$ mail
Mail version 8.1.2 01/15/2001. Type ? for help.
"/var/mail/rich": 2 messages 2 new
›N 1 atest@testbox Fri Feb 1 17:42 16/664 This is a test
N 2 atest@testbox Fri Feb 1 17:43 16/676 This is another
test
==> The mail command lists the status of mails in the mailbox.
& 1 ==> If you enter the number of the listed mail, the content of the mail is displayed.
Message 1:
From atest@testbox Fri Feb 1 17:42:56 2008
Date: Fri, 1 Feb 2008 17:42:56 -0500
From: atest@testbox
To: rich@localhost.localdomain
Subject: This is a test
This is a test message.
& d ==> Delete the email.
& q ==> Terminate the mail program.
Held 1 message in /var/mail/rich
$mail Barbara==> Write a mail to Barbara.
Subject: This is a test message
This is a test message that I’m sending to Barbara.
.
Cc:
$

Graphical e-mail clients

The programs we looked at earlier are text-based MUA programs. However, these days, various graphic-based MUA programs are provided, and the famous programs among them are as follows..

■ KMail for the KDE windows environment

■ Evolution for the GNOME windows environment

Setting up the mail server

Then, let's see how to set up the server to use the Aze mail server. Before sending mail, you need to make sure that the MTA is running on your Linux system..

sendmail

The main launcher is sendmail. It typically runs in background mode, listens for SMTP connections from remote mail servers, and forwards outbound messages from: ​

Except for sendmail, there are configuration files for running the MTA and its configuration option values. Please refer to the table below for this configuration file and options. ​

The configuration file manipulates the destination mail address into sendmail to determine where and how to forward the message. The default location for the configuration file is /etc/mail/sendmail.cf. ​

The sendmail.cf file is composed of rules that analyze the received message and consist of actions to be taken. The table below shows the types and purposes of configuration files used in sedmail.

There are 3 types of helpers defined in each configuration file above. Those helpers are as follows.

■ Class: Defines the syntax of a general message that helps the Rule to identify a specific type.

■ Macors : A value set to simplify input of long strings.

■ sendmail: Option definition for setting parameters for program operation. ​

A configuration file consists of a set of classes, macros, options, and rulesets. Each function is defined as a single line of text in the configuration file. ​

An action character at the beginning of a text line indicates how the line is used. The table below is a table explaining the characters and their uses.

Postfix

Postfix is ​​a popular MTA program like sendmail. Let's take a brief look at it here. Below is a diagram of Posix.

The master program is used to control the overall operation of Posix. And it is responsible for starting other processes in Posix. ​

The table below shows the types and descriptions of core programs.

The install.cf: configuration file can be used to retrieve used installation parameters.

main.cf : This is the configuration file that controls the operation of the Posix program. Below are the default settings file values.

# ===================================================================
#service type private unpriv chroot wakeup maxproc command + args
# (yes) (yes) (yes) (never) (50)
# ===================================================================
smtp inet n - n - - smtpd
pickup fifo n - n 60 1 pickup
cleanup unix - - n - 0 cleanup
qmgr fifo n - n 300 1 qmgr
rewrite unix - - n - - trivial-rewrite
bounce unix - - n - 0 bounce
defer unix - - n - 0 bounce
trac unix - - n - 0 bounce
verify unix - - n - 1 verify
flush unix n - n 1000 0 flush
proxymap unix - - n - - proxymap
smtp unix - - n - - smtp
relay unix - - n - - smtp -o fallback_relay=
showq unix n - n--showq
error unix - - n--error
local unix - n n--local
virtual unix - n n - - virtual
lmtp unix - - n - - lmtp
anvil unix - - n-1anvil
scache unix - - n - 1 scache

Sending a Message with Mailx

The main tool you can use to send e-mail messages from a shell script is Mailx. program. You can use it interactively to read and send messages, as well as specify how messages are sent. ​

The syntax for using mailx is as follows, and the available parameters are as shown in the table below.

■mail [-eIinv] [-a header] [-b addr] [-c addr] [-s subj] to-addr

Below are some examples of using mail programs.

$ echo "This is a test message" | mail -s "Test message" rich
$ mail
Mail version 8.1.2 01/15/2001. Type ? for help.
"/var/mail/rich": 1 message 1 new
›N 1 rich@testbox Fri Feb 1 19:12 16/664 Test message
&
Message 1:
From rich@testbox Fri Feb 1 14:12:03 2008
Date: Fri, 1 Feb 2008 19:12:02 -0500
From: rich ‹rich@testbox›
To: rich@localhost.localdomain
Subject: Test message
This is a test message
&
$ cat factmail
#!/bin/bash
# mailing the answer to a factorial
MAIL=`which mail`
factorial=1
counter=1
read -p "Enter the number: " value
while [ $counter -le $value ]
do
factorial=$[$factorial * $counter]
counter=$[$counter + 1]
done
echo The factorial of $value is $factorial | mail -s "Factorial
answer" $USER
echo "The result has been mailed to you."
$ ./factmail
Enter the number: 5
The result has been mailed to you.
$
$ mail
Mail version 8.1.2 01/15/2001. Type ? for help.
"/var/mail/rich": 1 message 1 new
›N 1 rich@testbox Fri Feb 1 19:24 16/671 Factorial answer
&
Message 1:
From rich@testbox Fri Feb 1 14:24:33 2008
Date: Fri, 1 Feb 2008 19:24:33 -0500
From: rich ‹rich@testbox›
To: rich@localhost.localdomain
Subject: Factorial answer
The factorial of 5 is 120
&

Let's look at an example of sending a longer e-mail.

$ cat diskmail
#!/bin/bash
# sending the current disk statistics in an e-mail message
date=`date +%m/%d/%Y`
MAIL=`which mail`
TEMP=`mktemp tmp.XXXXXX`
df -k › $TEMP
cat $TEMP | $MAIL -s "Disk stats for $date" $1
rm -f $TEMP
$ ./diskmail rich
$ mail
Mail version 8.1.1 6/6/93. Type ? for help.
"/var/spool/mail/rich": 1 message 1 new
›N 1 rich@test2.dfas.mil Mon Feb 3 14:15 15/594 "Disk stats
for 02/03/"
&
Message 1:
From rich Mon Feb 3 14:15:57 2008
Delivered-To: rich@test2.dfas.mil
To: rich@test2.dfas.mil
Subject: Disk stats for 02/03/2008
Date: Mon, 3 Feb 2008 14:15:57 -0500 (EST)
From: rich@test2.dfas.mil (Rich)
Filesystem 1k-blocks Used Available Use% Mounted on
/dev/hda1 3526172 1464476 1882576 44% /
/dev/hda6 16002168 6570168 8619116 43% /home
&

Using the mutt program.

Mutt is a program that can send attachments to the client program. ​ The installation method is as follows.

1) Download the latest version from the www.mutt.org site. ​

2) Decompress the compression using the command below. tar -zxvf mutt-1.4.2.3.tar.gz ​

3) Move to the newly created directory. cd mutt-1.4.2.3 ​

4) Execute the configure file to set parameters for execution. ./configure ​

5) Execute make to compile. make ​ Using mutt Let's see how to use mutt and an example. ​

How to use is as follows. mutt -s Subject -a file recipients ​ Let's look at a usage example..

# echo "Here’s the log file" | mutt -s "Log file" -a /var/log/messages rich

The table below is an option table for mutt command parameters.

728x90
반응형

'Shell Script' 카테고리의 다른 글

Chapter 21 Shell Script for Adminisrator  (54) 2023.06.22
Chapter 19 Using Web  (117) 2023.06.20
Chapter 18 using Database  (57) 2023.06.19
17 Advance gawk  (4) 2022.09.03
16 Advanced Sed  (1) 2022.08.31