Chapter 5 study notes of Linux command line and shell script programming

Source: Internet
Author: User
Tags bbcode
Chapter 25th: Using E-mailLinuxE-mail in Linux, the E-mail process is generally divided into three functions: MailTransferAgent (MTA) mailDeliveryAgent (MDA (

Chapter 2: Use e-mail

Linux E-mail Basics

E-mail in Linux

In Linux, the E-mail process is generally divided into three functions:

Mail Transfer Agent (MTA)

Mail Delivery proxy (Mail Delivery Agent, MDA)

Mail User Agent (MUA)

Some E-mail software combines MDA and MTA, and some combine MDA and MUA

Email sending proxy

MTA is responsible for sending and receiving emails in the system

For each sent message, the MTA must determine the recipient address.

If the destination is a local system, MTA will send it directly to the local mailbox or send the message to the local MDA for delivery

If the destination host is a remote email server, the MTA must establish a communication connection with the MTA on the remote host for transmission.

MTA usually uses two methods to deliver mail to a remote host:

Direct delivery)

Proxy delivery)

If the Linux system is directly connected to the Internet, it can directly deliver messages sent to remote host recipients to the remote host,

MTA uses the Domain Name System (DNS) to resolve the IP address, and then uses the Simple Mail Transfer Protocol (SMTP) to establish a network connection.

If Linux is not directly connected to the Internet or does not want to communicate directly with other remote hosts, it usually uses a front-end host (smart host ).

The front-end host is a proxy server that receives mail messages from the Linux system and then tries to deliver them directly to the target recipient.

Rogue servers can send spam by sending Unsolicited illegal cial Email (UCE) messages through front-end hosts. They can also hide their identities. "Now" most front-end hosts require some form of authentication before forwarding messages to other hosts.

For received emails, the MTA must be able to accept connection requests from the remote mail server and receive messages sent to local users. The most common protocol in this process is SMTP.

Mail delivery agent

MDA only focuses on delivering emails to local users

There are three common types of user mailboxes in Linux:

/Var/spool/mail or/var/mail file

$ HOME/mail file

Maildir-style email directory

Maildir is a relatively new feature, which is supported by some advanced MTA, MDA, and MUA applications. A mailbox is a directory in which each message is a separate file instead of each message as part of the mailbox file.

Almost all MDA and MUA can use the/var/spool/mail email file.

Email User proxy

MUA does not receive messages and only displays messages already in the mailbox.

Many MUA can send emails (either by using the SMTP front-end host function or automatically delivering messages to the local MTA)

Some popular MUA programs:

Mailx

Mailx can only read messages in/var/mail or $ HOME/mail format.

Mailx can also send emails.

$ Mail su1216Cc: Subject: testthis is a test. $ mail "/var/mail/su1216": 1 message 1 new> N 1 su1216 March 16: 15/640 test? Return-path:
 
  
Envelope-to: su1216 @ su1216-OptiPlex-380Delivery-date: Wed, 20 Nov 2013 16:24:32 + 0800 encoded Ed: from su1216 by su1216-OptiPlex-380 with local (Exim 4.71) (envelope-from
  
   
) Id 1Vj35L-0005Bj-WFfor su1216 @ su1216-OptiPlex-380; Wed, 20 Nov 2013 16:24:32 + 0800 Date: Wed, 20 Nov 2013 16:24:32 + 0800Message-Id:
   
    
To:
    
     
Subject: testX-Mailer: mail (GNU Mailutils 2.1) From: su1216
     
      
This is a test.
     
    
   
  
 

After completing the email, use ctrl + d to end the email.

Mutt(Using the ncurses library)

Graphical E-mail client

In the KDE window environmentKMail

In the GNOME window environmentEvolution

The above two are of little use in shell script programming.

Create a server

Sendemail

Sendemail usually runs in background mode. it listens to SMTP connections from the remote mail server and forwards emails from local users.

Sendemail configuration file

File Description
Sendemail. cf Text files that control sendemail program behavior
Sendemail. cw Text file containing the domain name list used by the sendemail program to receive messages
Sendemail. ct Text files containing a list of trusted users that can control sendemail running
Aliases Contains a list of valid local email addresses that can redirect emails to another user, file, or program.
Newaliases Execute programs for creating new aliases database files from text files
Mailq Executable program for checking the mail queue and printing any messages
Mqueue Directory used to store messages to be delivered
Mailertable Text file used to specify the route path of a specific domain
Domaintable Text file used to map the old domain name to the new domain name
Virtustable Text files used to map users and domains to alternate addresses
Relay-domains Used to list text files of a specific host that can forward messages through the sendemail program
Access Lists text files of specific domains. messages from these domains are allowed or disabled.

Sendemail. cf file

This configuration file tells sendemail how to process the target email address to determine how to forward these messages and where to forward them.

The default file location is/etc/mail/sendemail. cf.

Sendemail. cf is composed of rule groups.

The rule can contain helper programs defined in the configuration file:

Class: defines some common phrases to help organize messages of a specific type.

Macro: set some values to simplify the input of long strings in the configuration file.

Option: set parameters to control sendemal program running.

The configuration file consists of a series of classes, macros, options, and rule groups. Each function is defined in the configuration file in the form of a single text line.

Each line in the configuration file starts with a single letter that defines the command for this line. A line starting with a space or a tab character is a continuation of the previous command line. The line starting with the pound sign is a comment

Sendemail configuration file command

Configuration command Description
C Define text classes
D Definition macro
F Define a file containing text classes
H Define header fields and commands
K Define a database containing the text to be searched
M Define email sending proxy
O Define sendmail options
P Defines the priority of sendmail.
R Rule groups that define resolution addresses
S Define the set of rule groups

Postfix

Developed by Wietse Venema, Postfix provides a backup MTA for Unix-type servers

Postfix uses a main program that is always running as a background process. The main program uses other help programs that will be started as needed based on their functions.

Postfix help program

Program Description
Bounce Send a log to the returned message queue and send the returned message back to the sender.
Cleanup Process the received mail header and put the message in the receiving queue
Error Process message delivery requests from qmgr and force message return
Flush Process messages waiting for extraction by the remote email server
Local Messages delivered to local users
Pickup Wait for messages in the maildrop queue and send them to the cleaner to start processing.
Pipe Forward messages from the queue manager program to an external program
Postdrop When a common user has no write permission for this queue, the received message is moved to the maildrop queue.
Qmgr Processes messages in the receiving queue, determines where and when they should be delivered, and starts the program during delivery
Sendmail Provides a sendmail-compatible interface for the program to send messages to the maildrop queue.
Showq Report the status of the Postfix Message Queue
Smtp Use SMTP protocol to forward messages to external mail hosts
Smtpd Use SMTP to receive messages from external email hosts
Trival-rewrite Receive messages from the cleanup program. ensure that the header address of the qmgr program complies with the standard format and is used by the qmgr program to parse the remote host address.

When processing messages, Postfix uses different message queues to manage E-mail messages. Each message queue contains messages in different message states in the Postfix system.

Postfix Message Queue

Queue Description
Maildrop Receive new messages from local users to be processed
Incoming Receives New messages to be processed from the remote host and processed messages from local users.
Active Prepare the message delivered by Postfix
Deferred The first delivery failed, waiting for the second delivery of the message
Flush The message sent to the remote host. the remote host connects to the email server to obtain the message.
Mail Saved delivered messages for local users to read

If the Postfix system is disabled at any time, messages are still stored in the queue where they were last placed. After the Postfix is restarted, it automatically starts to process messages from these queues.

Postfix configuration file

Postfix uses three independent configuration files to allow you to set parameters used to guide Postfix in message processing. Unlike some MTA, it can modify the configuration information when the Postfix is running, and then run a command to load the new configuration for the Postfix without stopping the email server completely.

These three files are usually located at/etc/postfix

Postfix configuration file

File Description
Install. cf Contains the installation parameter information used to install Postfix
Main. cf Parameters used by the Postfix program to process messages
Master. cf Parameters used by the Postfix main program to run the core program

In the configuration file, each Postfix parameter and its value are in a separate row. the format is as follows:

Parameter = value

If you need to specify a front-end host, you can use the relayhost parameter, for example:

Relayhost = myisp.com

You can also specify an IP address here, but it must be enclosed in square brackets.

Use Mailx to send messages

The command format for sending messages by the Mailx program is:

Mail [-eIinv] [-a header] [-baddr] [-c addr] [-s subj] to-addr

Mailx command line parameters

Parameters Description
- Specify the rows in the additional SMTP header
-B Add a BCC recipient to the message.
-C Add a CC recipient to the message
-E Do not send messages if the message is empty.
-I Ignore TTY interrupt signal
-I Force Mailx to run in interactive mode
-N Do not read the start file of/etc/mail. rc
-S Specify a title line
-V Display delivery details on the terminal

The following is an example of sending an email. of course, we can also save the body to the text:

$ MailNo mail for su1216 $ echo "test" | mail-s "Test message" su1216 $ mail "/var/mail/su1216 ": 1 message 1 new> N 1 su1216 4 May 16: 15/637 Test message? Return-path:
 
  
Envelope-to: su1216 @ su1216-OptiPlex-380Delivery-date: Thu, 05 Dec 2013 16:47:06 + 0800 encoded Ed: from su1216 by su1216-OptiPlex-380 with local (Exim 4.71) (envelope-from
  
   
) Id 1VoUaQ-0005tl-Dlfor su1216 @ su1216-OptiPlex-380; Thu, 05 Dec 2013 16:47:06 + 0800 Date: Thu, 05 Dec 2013 16:47:06 + 0800Message-Id:
   
    
Subject: Test messageTo:
    
     
X-Mailer: mail (GNU Mailutils 2.1) From: su1216
     
      
Test?
     
    
   
  
 

Mutt program

Developed by Michael Elkins in 1995

Mutt can send a file as an attachment in an E-mail message. Mailx can only be sent as the body.

Mutt command line

Mutt command line parameters

Parameters Description
-A alias Pass the expanded version of the specified alias to STDOUT.
-A file Attaches a specified file to a message using the MIME protocol.
-B address Specify a BCC (Blind Carbon Copy, secretly cc) recipient
-C address Specify a CC (Carbon Copy, CC) recipient
-D Print all configuration option values to STDOUT
-E command Specifies the configuration command to run after processing the initialization file
-F mailbox Email file to be loaded
-F muttrc Specify the initialization file to be read, instead of $ HOME/. muttrc
-H Show help text
-H draft Specify a draft file containing the title and body to send messages.
-I include Specify a file to be included in the message body.
-M type Specify the Default mailbox type
-N Ignore system configuration files
-P Restore an expired message
-Q query Queries a configuration variable value. The query will be executed after all the configuration files are parsed and any commands specified on the command line are executed.
-R Open email in read-only mode
-S subject Message title
-V Display Mutt version number and compile-time definition
-X Simulate Mailx editing mode
-Y Started with the list of all mailboxes specified by the email command
-Z When used together with-f, it is not started if there is no message in the mailbox
-Z Open the first mailbox containing the new email specified by the email command

Use Mutt

Basic command format:

Mutt-s Subject - File -- Recipients

The recipient list is separated by spaces.

The file must be an absolute path or a relative path of the current working directory based on the mutt command. You can add multiple files after-a to separate them with spaces.

echo "email content: test!" | mutt -s "A mail from su1216" -- su1216

Repost the following link

My blog address

Http://su1216.iteye.com/

Http://blog.csdn.net/su1216/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.