linux send email from command line

Original text
Ubuntu server 14.04 Send mail from the command line
Install mailutils
sudo apt-get installutil mails < /span> linux sends mail from the command line You need to remember the System mail name here, which will be used later. linux sends mail from the command line linux sends mail from the command line
Next, just perform the default installation. Postfix Configuration



Send an email
1. Use a one-line command to send an email
Send to the email address [email protected] (change it to the email address you want to send) email. The subject of the email is Test email from ubuntu server!, and the content is Here is the message body..
mail -s "Test email from ubuntu server!" [email protected] <<< 'Here is the message body.'
echo ' Here is the message body.' | mail -s “Test email from ubuntu server!” [email protected]
The above two commands can get the same effect. Among them, the -s parameter specifies the subject of the email.
After running the above command, go to the mailbox ([email protected]) to check whether you have received the email. If it's not in your inbox, it's probably in spam.

2. Use the mail command prompt to send an email.
Send an email to [email protected] and copy it to [email protected]. The subject of the email is Ubuntu Test and the content is Merry christmas
mail -s 'Test Subject' [email protected]
Enter this command and press Enter, prompting Cc: , then enter the carbon copy email address [email protected] and press Enter.
Continue to enter the email body content Merry christmas. After entering the body text, press Ctrl-D to end the input and send the email.

3. Read the email content from the file and send it
Send an email to the email address [email protected]. The email subject is Text message and the email content is /home/user/message. Contents in .txt.
mail -s ‘Text message’ [email protected] < /home/user/message.txt

4. Cc and Bcc
Send an email to [email protected], and copy it to [email protected], and send it Bcc to user3@qq. com.
mail -s 'Subject' [email protected] -c [email protected] -b [email protected] < message.txt
-c means carbon copy, -b means blind carbon copy. -c: carbon copy, -b: blind carbon copy.
[It seems that these two parameters are no longer available. ]

5. Send emails to multiple mailboxes
mail -s 'Subject' [email protected],[email protected],[email protected] < message.txt

6. Specify the sender’s name and address
echo “This is the message body” | mail -s “subject” [email protected] -aFrom:sender@qq. com
Use the -a parameter to append email header information to specify the sender's name and address.
or
echo “This is the message body” | mail -s “subject” [email protected] -aFrom:John\[email protected]\

7. Send mail to other users of this machine
mail -s “hello, this is a test!” username
mail -s “ hello, this is a test!” username@ubuntu
These two methods are equivalent. In the second method, ubuntu is the hostname of the current system. See the last picture in the installation section. Figure, the default system email name is the host name, which is also ubuntu.

Add attachment
echo “This is the message body” | mail -s “subject” [email protected] -A /path/to/attached_file
Use the -A parameter to add attachments to the email.
Or use another command line tool –Mutt.
-> Install
sudo apt-get install mutt
-> Use
Send a simple email
echo “This is a mutt test” | mutt -s “This is mutts subject” [email protected]
Send an attachment email: Note: The attachment file and the recipient need to be separated by –. -a parameter added Attachment (attach), followed by the path where the attachment is located; mailcontent.txt is the body content of the email.
mutt -s “Subject” -a /path/to/file_to_attached – [email protected] < /home/user/mailcontent.txt

Shell script sends email
Write a shell script to send hard disk usage information to the mailbox. Use du -sh to check hard disk usage.

!/bin/bash

filename: report_disk_usage_to_email.sh

du -sh | mail -s “disk usage report” [email protected]
As you can see in the third picture in Part 1 – [Installing mailutils], My yourmaildomain.com is ubuntu.
Exit editing after saving, and then run the script, the command is: sh report_disk_usage_to_email.sh.

Check mail
To check mail, enter mail directly in the command line and all mail statuses will be listed, as shown in the figure. If there are no unread messages, No mail for user is returned.
Linux sends emails from the command line
There are 7 emails displayed on the third line, 3 of which are unread.
The second column is the email number, the last column is the email subject, and the middle is the receipt date.
The last line ? prompt indicates waiting for command input.
Enter the email number and press Enter to open the email for reading.
Enter z and press Enter to return to the mailing list.
Enter q and press Enter to exit.

Guess you like

Origin blog.csdn.net/u012839224/article/details/78857003