Linux CentOS6 mail/sendmail command to send email implementation

Source: Internet
Author: User
Tags auth how to send mail mail exchange mx record

Because you often work under Linux, so many times you need to send your work reports or other useful stuff to the people involved, so take the time to study how to send mail under Linux. We can usually use the following 3 to send the way:

1. Use Shell to send mail as editor

This way you can edit the message body directly in the shell window, and when the edit is finished, use Ctrl+d to exit, and the message will be sent out.

The code is as follows Copy Code
#-S is followed by the subject of the message, followed by the destination mail address [root@centos6 ~]# mail-s "Lucky Mail" xlapn@126.com
Hi Tomas,
Thank for your help!
Regards,
Daniel2. Send a message using a pipe

The message body is behind Echo.

The code is as follows Copy Code

[Root@centos6 ~]# echo "Hi Tomas,
> Thank for your help!
> Regards,
> Daniel "| Mail-s "Thank you Mail" xlapn@126.com3. Send the message body as a file

This form is appropriate for a longer message body, writes the message body to the file mail.body, and then navigates to the target message via <

The code is as follows Copy Code
[Root@centos6 ~]# mail-s ' mail ' xlapn@126.com < Mail.body4. Send a message with an attachment

To send a message with an attachment, you need to install the Uuencode package, and you can use the following command to install the Uuencode package if the Yum source is configured.

The code is as follows Copy Code
[Root@centos6 ~]# yum install sharutils installed uuencode can send mail with attachments

# uuencode followed by two parameters, the first argument "/root/batch.file" is an attachment full path, the second argument "batch" is the name of the attachment in the message [ROOT@CENTOS6 ~]# uuencode/root/ Batch.file Batch | Mail-s "Batch file" Xlapn@126.com < mail.body some of the commands above have been tested on CentOS6.


Send mail using SendMail


SendMail is the client that is used to send mail under the Linux/unix system. SendMail uses the SMTP protocol to send messages to the destination SMTP server. The work flow is as follows:

Let's start with the MX record for DNS: SMTP servers are routed through e-mail messages based on MX (mail exchange) records in DNS, and MX records register domain names and associated SMTP relay hosts, and e-mail messages belonging to that domain should be sent to that host.
(1) Sendmail request DNS to give the host sh.abc.com CNAME records, if any, if CNAME (alias record) to shmail.abc.com, then again request shmail.abc.com CNAME record, until No.
(2) Suppose to be cname to shmail.abc.com, and then SendMail request the DNS of the @abc.com domain to give shmail.abc.com MX records (message routing and Logging), Shmail MX 5 shmail.abc.com 10 Shmail2.abc.com.
(3) The SendMail combination requests DNS to give the shmail.abc.com a record (hostname (or domain name) corresponding to the IP address record), that is, the IP address, if the return value is 10.0.0.4 (assumed value).
(4) SendMail and 10.0.0.4 Connect, send this letter to user@sh.abc.com to 1.2.3.4 the SMTP daemon for this server.

1. Construct Mail
Before you send a message using SendMail, you first need to construct a message by message format. Include message headers, mail message body. Message format in

  code is as follows copy code

RFC5322: The Internet Message Format (http://tools.ietf.org/html/rfc5322) is described in detail.
?
Code highlighting produced by Actipro Codehighlighter (freeware)
http://www. codehighlighter.com/

-->from:john Doe <jdoe@machine.example>

Sender:michael Jones < Mjones@machine.example>

To:mary Smith <mary@example.net>

subject:saying Hello

Date:fri , Nov 1997 09:55:06-0600

Message-id: <1234@local.machine.example>

 

This be a message Just to say hello.

So, "Hello".
 


The date header (mail delivery time) and MessageID can be temporarily unspecified, and this information will be filled out by the SendMail program.


2. Send mail using SendMail
After you have constructed the message, save it to a local file, such as/data/mail_content. Then call SendMail to send and specify the receive mailbox address when sending:
cat/data/mail_content | SendMail user@163.com

3. Send a message in HTML format
If you want to send a message in HTML format, that is, the message body of the message is an HTML file, you need to specify Content-type as text/html in the message header. If not specified, Content-type is text/plain, or normal text, by default.

The code is as follows Copy Code

Code highlighting produced by Actipro Codehighlighter (freeware) http://www. Codehighlighter.com/-->from:john Doe <jdoe@machine.example>
Sender:michael Jones <mjones@machine.example>
To:mary Smith <mary@example.net>
Content-type:text/html
Subject:saying Hello

<div style= "Border:solid 1px #1D448C;" >
<p>so, "Hello" .</p>
</div>

4. Character encoding
In sending a Chinese message, character encoding is a more important issue, and if it is not set correctly, the message header or message content will appear garbled.

    the encoding of the content of the message can be set in the message header Content-type, such as setting the message content to UTF-8 encoding:
    content-type:text/html ; Charset=utf-8
  
    headers, such as From,to,subject, if you need to use Chinese, you can set this:
    

  code is as follows copy code

" =? UTF-8? B? " +base64encode (Content UTF8 encoded) + "? ="

Code highlighting produced by Actipro Codehighlighter (freeware). Codehighlighter.com/-->from: =? UTF-8? b?5l2g5aw9?= <jdoe@machine.example>
Sender:michael Jones <mjones@machine.example>
To:mary Smith & Lt;mary@example.net>
Content-type:text/html;charset=utf-8
Subject: =? UTF-8? b?5l2g5aw9?=

<div style= "Border:solid 1px #1D448C;"
<p>so, "Hello" </P>
</div>

Above we also see SendMail mail send Configuration Super trouble, below we do not sendmail to send, instance


Bin/mail will use the local sendmail to send mail by default, which requires local machines to install and start the SendMail service, is cumbersome to configure, and can cause unnecessary

Resource occupancy. By modifying the configuration file, you can use an external SMTP server to send mail with an external SMTP server without using SendMail:

Modify/etc/nail.rc (/etc/mail.rc)

The code is as follows Copy Code
Set from=fromuser@domain.com smtp=smtp.domain.comset smtp-auth-user=username smtp-auth-password=passwordset Smtp-auth=login


Description

From is the mailing address sent

SMTP is the address of the external SMTP server that occurred

Smtp-auth-user is the user name authenticated by the external SMTP server

Smtp-auth-password is an external SMTP server authenticated user password

Smtp-auth is the way of email authentication

Once the configuration is successful, you can use the

You can send an e-mail to test:

The code is as follows Copy Code
Mail-s "Test" user@sohu.com <content.txt

After the-S is the message header, User@sohu.com is the recipient address, content.txt inside is the message body

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.