How to send system emails to root or superuser in Linux

Home > Search > How-to

The mail command can be used to send an email to a user's inbox. Before the mail command can be used, the mailx package will need to be installed.

[root@server1 ~]# apt-get install mailutils
[root@server1 ~]# yum install mailx


If using sendmail, this command will send an email.

[root@server1 ~]# mail -s "Hello World"


For example, this script sends an email to root. 

sender="From: Firstname Lastname"
subject="Hello World"
message="How are you today?"

mailx -a "$sender" -s "$subject" $recipient <<< $message


An if statement can be used to send conditional emails to the superuser. In this example, if foo equals 1, an email is sent to foo is set to 1, so when this script is executed, an email will be sent to

1  #!/bin/bash
2  foo=1
3  if [ $foo -eq 1 ]
4  then
5    echo "Hello World" | mailx -s "Test Email"
6  fi


More TBD . . . 

Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

Please enter in the box below so that we can be sure you are a human.