Configure the mail sending function in linux

Source: Internet
Author: User
Tags starttls

Configure the mail sending function in linux
1. mutt + msmtp installation by default, smokeping sends an email using sendmail, but sendmail configuration is really not a common problem, and it is not necessary, completely useless, so I want to use the mutt + msmtp solution to send alert emails. Install the mutt and msmtp software first. If the package is installed online, you can simply install apt-get install, which will automatically help us install the associated package. If it is in an offline environment, we can first install it in a wired environment. Then, install the downloaded installation package in an offline environment (this is because apt-get install downloads all installation packages and associated packages to the/var/cache/apt/archives directory), run the following command to install the deb installation package one by one.

sudo  dpkg  -i  package.deb

The following is an online installation method. We can see the dependency packages in the installation information. Remember the names of these dependency packages and copy them out.

bitnami@linux:/var/cache/apt/archives$ sudo apt-get install mutt[sudo] password for bitnami: Reading package lists... DoneBuilding dependency tree       Reading state information... DoneThe following extra packages will be installed:  libgpgme11 libpth20 libtokyocabinet8Suggested packages:  gpgsm urlview aspell ispell mixmasterThe following NEW packages will be installed:  libgpgme11 libpth20 libtokyocabinet8 mutt0 upgraded, 4 newly installed, 0 to remove and 50 not upgraded.Need to get 1,752 kB of archives.After this operation, 5,197 kB of additional disk space will be used. bitnami@linux:~$ sudo apt-get install msmtpReading package lists... DoneBuilding dependency tree      Reading state information... DoneThe following extra packages will be installed:  libgsasl7 libntlm0Suggested packages:  msmtp-mtaThe following NEW packages will be installed:  libgsasl7 libntlm0 msmtp0 upgraded, 3 newly installed, 0 to remove and 50 not upgraded.Need to get 265 kB of archives.After this operation, 1,201 kB of additional disk space will be used.Do you want to continue [Y/n]? 
2. Configure msmtp and mutt2.1 and configure msmtp to create the msmtp log File ". msmtp. log", which is specified in. msmtprc. Note that "." indicates a hidden file and the content is blank.
$ sudo vim ~/.msmtp.log

Configure the msmtp Configuration File ". msmtprc"

#Accounts will inherit settings from this sectiondefaults# A first gmail addressaccount        gmailhost           smtp.gmail.comport           587from           username@gmail.comuser           username@gmail.compassword       passwordtls_trust_file /etc/ssl/certs/ca-certificates.crt# A second gmail addressaccount    gmail2 : gmailfrom       username2@gmail.comuser       username2@gmail.compassword   password2# A freemail serviceaccount    freemailhost       smtp.freemail.examplefrom       joe_smith@freemail.exampleuser       joe.smithpassword   secret# A provider's serviceaccount   providerhost      smtp.provider.example# A 126 emaliaccount    126host       smtp.126.comport       25from       aaa@126.comauth       logintls        offuser       aaa@126.compassword   passwordlogfile    ~/.msmtp.log# Set a default accountaccount default : 126

Configure the. msmtprc permission. The following settings only give the. msmtprc user the read and write permissions. Others do not have any permissions.

$ Sudo chmod 600. msmtprc -- Set Configuration File Permissions

To view the users of. msmtprc, run the following command. We can see that the. msmtprc file owner is the root user and the group is the root group.

root@BJCGNMON01:~# ls -l .msmtprc -rw------- 1 root root 251 Feb 17 10:22 .msmtprc

The preceding settings are very important. If you use an account to call msmtp, the Account must have read and write permissions on the. msmtprc file.

2.2 The configuration of muttmutt can be divided into two types, to see whether you want to take effect globally or a single user. For global system settings, modify the/etc/Muttrc configuration file. If you are using a system user, you can modify "~ /. Muttrc "file.
#sudo vim ~/.muttrcset sendmail="/usr/bin/msmtp"set use_from=yesset realname="name"set from=aaa@126.comset envelope_from=yes

I only want to configure the mutt function for my current root user, so use the latter. After the modification, you also need to view the read and write permissions of the file. If the root account currently uses the mutt function, the. muttrc must have the read and write permissions for the root account. The permission can be viewed as follows:

root@BJCGNMON01:~# ls -l .muttrc -rw-r--r-- 1 root root 122 Feb 17 10:27 .muttrc
3. Test smtp information 3.1msmtp Test

Test command:

Test configuration file: msmtp-P test smtp server: msmtp-S

Another method is to perform the test before configuring msmtp. For example, the smtp command for testing 163 is as follows:

bitnami@linux:~$ msmtp --host=smtp.163.com --serverinfoSMTP server at smtp.163.com (smtp.163.gslb.netease.com [220.181.12.18]), port 25:    163.com Anti-spam GT for Coremail System (163com[20121016])Capabilities:    PIPELINING:        Support for command grouping for faster transmission    STARTTLS:        Support for TLS encryption via the STARTTLS command    AUTH:        Supported authentication methods:        PLAIN LOGINThis server might advertise more or other capabilities when TLS is active.

We can see from the returned information that this smtp supports TLS, and the authentication method supports PLAIN and LOGIN.

3.2 enter the following command line to test the mail:
echo "test" |mutt -s "my_first_test" aaa@126.com

If there are multiple recipients, separate them with spaces or commas. Test the command:

echo "test" |mutt -s "my_first_test" aaa@126.com bbb@163.comecho "test" |mutt -s "my_first_test" aaa@126.com,bbb@163.com
(PS: windows Mail Client blat)

For the mail sending client blat in windows, you can only use commas to separate multiple mail lists. The test command is as follows:

Blat-install 163.smpt.com aaa@163.com -- register blat % varlogfile %-to aaa@126.com, aaa@163.com "-u" aaa "-pw" aaa "-subject" content "-attach % varlogfile % -- send

 

We use the content after echo as the body of the email. You can also write the content of the email in a file and then send the content of the file.

Touch mail.txt -- create the mail text vim mail.txt -- edit the text content this is my first test email -- text content

Send mail, the following example is to send a message titled linkmail, the recipient is The aaa@126.com bbb@163.com, the attachment is/root/sent, and the mail content is the content in mail.txt. The sending script is as follows:

mutt -s "linkmail" aaa@126.com bbb@163.com  -a /root/sent 
4. configure global msmtp and mutt1. create/var/log/msmtp. log
touch /var/log/msmtp.log

To allow all users to read and write this log file, we set the permission to 777.

chmod 777 /var/log/msmtp.log

To modify individual permissions of users, groups, and others, run the following command. U: user, g: group, o: other.

chmod u+rwxchmod g+rwxchmod o+rwx

2. Create the msmtp configuration file/etc/msmtprc

touch /etc/msmtprc

3. Configure msmtprc

#Accounts will inherit settings from this sectiondefaults# A 126 emaliaccount    126host       smtp.126.comport       25from       aaa@126.comauth       logintls          offuser       aaa@126.compassword   passwordlogfile     /var/log/msmtp.log# Set a default accountaccount default : 126

4. Configure the global configuration file/etc/Muttrc for mutt, and add the following information at the end:

Set sendmail = "/usr/bin/msmtp" # This address is installed by default based on the actual configuration. Set use_from = yeset realname = "name" set from = aaa@126.comset envelope_from = yes

5. Test

When the test command is executed, a copy of the email is automatically written to "~ /Sent.

echo "test" |mutt -s "my_first_test" aaa@126.com

The root user sends an email, which is saved in/root/sent. The enadmin account sends the email and the email is saved in/home/enadmin/sent. For other users similar to enadmin, you must first create the/home/username directory and then modify its permissions.

mkdir /home/nagioschown -R nagios.nagios /home/nagios
Send an email by testing mutt. The/home/nagios/sent file will be automatically mounted. 6. FAQs:

Error 1: msmtp: account default not found: no configuration file available
Msmtp has a bug. You must manually specify the corresponding configuration file
In/etc/Muttrc, set sendmail = "/usr/bin/msmtp" to set sendmail = "/usr/bin/msmtp-C. msmtprc"
Error 2: msmtp: gnu sasl: Base 64 coding error in SASL library
Base64 encoding Error
Change ~ /. Auth login in msmtprc
Auth plain

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.