SoFunction
Updated on 2025-03-10

Brief analysis of how to use scripts to automatically send text mail messages under Linux

1. Install msmtp

luther@gliethttp:~$ sudo apt-get install msmtp

2. Edit the configuration script (~/.msmtprc is the default configuration file, and you can also use the -C option to specify the configuration file path)

luther@gliethttp:~$ vim msmtprc
#Add the following content
defaults
#logfile /home/luther/
account luther_test
host
from luther@
auth off
password 123456789
account default : luther_test

3. Remove the execution permissions of the configuration file -x

luther@gliethttp:~$ chmod 600 msmtprc

4. Okay, you can use the following statement to send a test email to your email address, or enter it on stdin, and then ctrl+D

Maybe some data in the file will be lost. Don't be afraid. Everything will be fine after we install mutt.
luther@gliethttp:~$ msmtp luther@ -C /home/luther/msmtprc < /vobs/tmp/

5. Then install mutt

luther@gliethttp:~$ sudo apt-get install mutt

6. Edit the configuration script (~/.muttrc is the default configuration file, and you can also use the -F option to specify the configuration file path)

luther@gliethttp:~$ vim muttrc
#Add the following content
set sendmail="/usr/bin/msmtp -C /home/luther/msmtprc"
set use_from=yes
set from=admin@
set realname="test admin"
set editor="vim"

7. Send plain text and file content

luther@gliethttp:~$ echo "Test 123abc" | mutt -F /home/luther/muttrc -s "Email Title - Test" luther@
luther@gliethttp:~$ mutt -F /home/luther/muttrc -s "Mail Title - Test" luther@ < /etc/passwd

8. Send attachments (the -a option is used to add attachments. The following example adds passwd and these 2 attachments)

luther@gliethttp:~$ echo "Test 123abc" | mutt -F /home/luther/muttrc -s "Mail Title - Test" luther@ -a /etc/passwd -a /etc/

9. Send bcc address and cc cc address. If multiple email addresses are separated by commas

luther@gliethttp:~$ echo "Test 123abc" | mutt -F /home/luther/muttrc -s "
Email Title - Test "luther@,@ -b @,luther@ -c @,luther@