L05-Linux deploys msmtp+mutt to send mail

I. Preface 
First of all, you must understand the process of sending an email. The following theory is excerpted from Liao Xuefeng's official website https://www.liaoxuefeng.com/article/00137387674890099a71c0400504765b89a5fac65728976000 
""" 
We usually use Email very easily, but the Internet's The mail system is connected by several complex parts. For end users, Outlook, Foxmail, etc. that we are familiar with are used to receive and send letters, which is called MUA : Mail User Agent, mail user agent. 
MUA does not send mail directly to the recipient, but through MTA : Mail Transfer Agent, mail transfer agent, Sendmail and Postfix play the role of MTA. 
After a mail is sent from MUA, it may pass through one or more MTA delivery, and finally arrives at MDA : Mail Delivery Agent, mail delivery agent, after the mail reaches the MDA, it is stored in a file or a special database, and we call this long-term storage place for mail as a mailbox. 
Once the mail arrives at the mailbox, Leave it in place and wait for the user to take it away through MUA, which is the process of receiving emails with software such as Outlook, Foxmail, etc. 
So the process of an email is: 
Sender: MUA --Send --> MTA - > Several MTAs... -> MTA -> MDA < 
--Receive-- MUA: The protocol used between the recipient MUA and MTA and between MTA and MTA is the SMTP protocol. When receiving mail, the most commonly used protocol between MUA and MDA is POP3 or IMAP. 
It should be noted that professional mail service providers have a large number of machines to serve users, so usually MTA and MDA are not the same server. Therefore, in software such as Outlook, we need to fill in the address of the SMTP sending server and the POP3 receiving server respectively. the address of. 
""" 
  
This blog post also introduces the process of sending emails very clearly: https://www.cnblogs.com/DLHe/p/8457393.html 
As can be seen from the above, the Outlook, Foxmail, etc. that we are familiar with are usually The mail client provides the function of MUA, while some Email service providers, such as NetEase, QQ, etc., provide the function of MTA. 
It must be clear that the msmtp+mutt deployed this time is actually similar to Foxmail. The MUA mail client of linux, but it runs on linux. If sendmail or postfix is ​​deployed on linux, that is the mail server, which plays the function of MTA.  
  
The next step is the implementation. The  
actual measurement is successful Deployment environment: RHEL6.5 and CentOS6.5, of course, the machine must be connected to the Internet. 
  
Second, install msmtp 
msmtp is an SMTP client that can send emails to the SMTP server, the official website address is http://msmtp.sourceforge .net/index.html.1. 
Download the msmtp file 
cd /usr/local/src 
wget http://sourceforge.net/projects/msmtp/files/msmtp/1.4.32/msmtp-1.4.32.tar.bz2/ download 
  
2. Unzip the file 
tar jxvf msmtp-1.4.32.tar.bz2  
  
3. Enter into the decompressed file, and then there are 3 steps of typical source installation./configure, make and make install  
3.1 Execute ./configure to generate Makefile, which is the next step To prepare for compiling, --prefix= is followed by the path where you want to install msmtp, which has nothing to do with the directory where the current mstmp source code is located  
cd msmtp-1.4.32 
./configure --prefix=/usr/local/msmtp  3.2 make &&  make install, compile and install, these two steps can also be executed separately Basic Information. mkdir –p /usr/local/msmtp/etc  vim /usr/local/msmtp/etc/msmtprc #Add the  following content: account default   host smtp.163.com                      port 25  from [email protected] //Fill in the truth according to the actual situation 163 mailbox  auth login  tls off  user xxx         
 
 
 
 
 
  
 


 






 //The email username is xxx instead of [email protected]              
password xxx //It should be noted that the password here may be an authorization code , such as 163 mailbox, the authorization code must be filled here, as shown in the following figure           
logfile /var/log /mmlog  Note: As mentioned earlier, the combination of msmtp+mutt is a mail client just like Foxmail. We know that if you use Foxmail to send emails under Windows, of course, you first need to log in to Foxmail with one of your accounts, and then you can create new emails to send under the client. Then the function of this file configuration in this step, my understanding is to log in to the msmtp client with one of your accounts under linux. Here I use 163 NetEase mailbox, if you want to use QQ mailbox, you need to modify it separately. The configuration during the test is as follows: (Note: The directory in the screenshot is a bit confusing, it is the installation directory deployed in the actual production environment, not the /usr/local/msmtp specified by the --prefix parameter when the ./configure command is executed above, but it doesn't matter, Just remember, the file is actually in the installation directory specified by the prefix parameter!)  5.  Give permissions to the log file: chmod 777 /var/log/mmlog  6. Give permissions to the configuration file msmtprc because there are The plaintext password of the mailbox, so it should be set to be non-readable. chmod 600 msmtprc  7.
 
  

  
 
 

  
 

  
 

  
At this time, you can make a soft link about /usr/local/msmtp/bin/msmtp in the /usr/bin/ directory, so that you can directly type the msmtp command anywhere on the server to execute msmtp  
ln -s /usr/local/msmtp /bin/msmtp /usr/bin/msmtp  
  
8. Test msmtp (similarly, the email address depends on the actual situation, but it must be the same as the email address in the above /usr/local/msmtp/etc/msmtprc configuration file )  
( 1) If the soft connection in step 7 is not done, it is:  
/usr/local/msmtp/bin/msmtp xxx@ 163 .com  hello, test ctrl d 
(2) If the soft connection is made, the test command can be :  
msmtp xxx@ 163 .com  hello, test ctrl d 
#Check  whether the log is successful  cat /usr/local/msmtp/msmtp.log #Check 
the  163 mailbox to see if the letter is received.  
As shown in the figure below, the email can be received at this time, but the email has no content, which is no problem.  
At this point, the msmtp installation has been completed.  3. Install Mutt
 
  
  
 
Mutt is an Email client, it can't send emails directly, it is more responsible for the management function of email, it can be used with msmtp. The official website address is http://www.mutt.org/  
If you want to know more about the use of Mutt and msmtp, you can check the following address: http://msmtp.sourceforge.net/doc/msmtp.html#Using-msmtp- with-Mutt   1. Mutt
  
installation method 1: yum installation and configuration of mutt (measured successfully)  
1.1 yum install -y mutt  
  
1.2 Edit the /etc/Muttrc file  ( Note, the Muttrc file should already exist in the /etc directory, just append it at the end of the file The following content) 
vim /etc/Muttrc #Enter 
the following content: 
set sendmail="/usr/bin/msmtp"      #Note : The reason why this can be written is because a soft connection was made when configuring msmtp  #ln -s /usr/local /msmtp/bin/msmtp /usr/bin/msmtp 
set use_from=yes 
set realname="YOURNAME"          #realname can be set arbitrarily, so that when you send an email to others, the recipient sees that the sender's title is you Set here, you can see the following settings and the results shown in the last picture 
set [email protected]  
envelope_from=yes 
auto_view text/html 
set charset="utf-8" 
set send_charset="utf-8" 
set locale="zh_CN.UTF-8" 
set content_type = "text/html\;charset=utf-8" 
  
set rfc2047_parameters =yes 
The configuration of the actual measurement is as follows:  But when doing the test of sending mail, it is wrong. It can send mail, but after executing the sending command, an error is reported: source: errors in /etc/Muttrc, so change it to the following set sendmail="/usr /bin/msmtp"  set use_from=yes  set realname="Zabbix Server"  set editor="vim"  set [email protected]  1.3 After configuration, test: (1) Simple test (send to yourself): echo "TEST Test test"|mutt -s "DT_ZABBIX_SERVER_TEST" [email protected]  (2) Add attachment test (send to yourself): echo "Content: TEST test test"|mutt -s " 主题:DT_ZABBIX_SERVER_TEST" -a /data/suxy_remark.txt [email protected] 
 
 





 
  
 

 
  

Send error, report error : Can"t stat [email protected]: No such file or directory 
as shown in the figure below, this is because [email protected] is behind the -a command, so mutt also regards [email protected] as an attachment , so the -a command is swapped and placed at the end (in fact, the usage of this command does require the -a parameter to be placed at the very end of the command): 
echo "Content: TEST test test"|mutt -s "Subject: DT_ZABBIX_SERVER_TEST" [email protected] -a /data/suxy_remark.txt is 
sent successfully! 
In fact, you can also use the following command, that is, write the mailbox after mutt first, and put the -s, -a and other parameters in the back to 
echo "content: TEST test test"|mutt [email protected] -s "Subject: DT_ZABBIX_SERVER_TEST" -a /data/readme.txt  (3) Of course, you can also send emails to other types of mailboxes, such as qq mailboxes, company mailboxes, etc .:  echo "Content: TEST test"|mutt -s "Subject: DT_ZABBIX_SERVER_TEST" [email protected]  -a /usr/local/suxy_remark.txt It's the wrong test practice..) cd /usr/local/src 
 
  


 
  
  
 

wget ftp://ftp.mutt.org/pub/mutt/mutt-1.5.24.tar.gz 
tar xvf mutt-1.5.24.tar.gz 
cd mutt-1.5.24 
./configure --prefix=/usr /local/mutt  make  &&  make install configure Mutt vim /usr/local/mutt/etc/Muttrc #Add  the following lines at the end:  set sendmail="/usr/local/msmtp/bin/msmtp"  set use_from=yes  set realname ="xxx"  set [email protected]  set envelope_from=yes  test Mutt echo "hello,world"|/usr/local/mutt/bin/mutt –s "test mail" [email protected]#The above statement will Send an email with the title "test mail", the content "hello,world", and the recipient [email protected], you can check whether the mailbox can receive the email  The command used in the actual test:  echo "Test from Zabbix Server" |/usr/local/mutt/bin/mutt –s "zabbixTest" [email protected] 
 
 
  
 
 
  
  
 







  
 



But if you use the above command to send, it will send an email to each of [email protected] and [email protected] and [email protected]. If you change to the following command, it will only be sent to 182, but no Subject 
echo "Test from Zabbix Server"|/usr/local/mutt/bin/mutt [email protected]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325561001&siteId=291194637