How to Send Postfix Email With a Custom Form Name Using the -R Option

When it comes to sending email notifications from a server, it’s important to convey information in a clear and recognizable manner. In this article, we’ll explore how to send a Postfix email with a custom form name using the -r option. This will allow us to define a sender name that helps recipients easily identify the source of the email. We’ll go through the process step by step, explaining each component and its significance.

Prerequisites

Before we begin, ensure you have the following:

  • A Unix-like operating system (e.g., Linux)
  • Postfix installed and configured on your server
  • Basic command-line knowledge

The Command Explained

The command you provided uses the echo command to generate the content of the email body. It includes information such as the IP address ($IP), hostname ($HOSTNAME), and current date and time ($NOW). The mailx command is then used to send the email.

Here’s the breakdown of the command:

1
echo 'Someone from '$IP' logged into '$HOSTNAME' on '$NOW'.' | mailx -s -r 'EXAMPLE-PC' 'SSH Login Notification' [email protected]
  • echo '...': This part of the command generates the content of the email body. It uses the variables $IP, $HOSTNAME, and $NOW to incorporate the relevant information into the message.
  • mailx -s -r 'EXAMPLE-PC' 'SSH Login Notification' [email protected]:
    • mailx: The command-line utility used to send emails.
    • -s: Specifies the subject of the email.
    • -r 'EXAMPLE-PC': This is where the customization takes place. The -r flag is used to set the sender’s name. In this case, 'EXAMPLE-PC' is the custom sender name you want to use.
    • 'SSH Login Notification': The subject of the email.
    • [email protected]: The recipient’s email address.

Converting to Postfix-Compatible Format

To ensure the command is compatible with the Postfix email system, make sure you format it correctly. The -r flag might not be recognized by all versions of mailx. As an alternative, you can use the -a flag to add a “From” header with the desired sender name. Here’s the adjusted command:

1
echo 'Someone from '$IP' logged into '$HOSTNAME' on '$NOW'.' | mailx -s 'SSH Login Notification' -a 'From: EXAMPLE-PC' [email protected]

In this version of the command, we use the -a flag to add a custom “From” header with the sender name 'EXAMPLE-PC'.

Conclusion

By using the -r or -a option in the mailx command, you can customize the sender name for the email notifications you send from your server. This small adjustment can greatly enhance the clarity and recognition of your email communications.

0%