How to Change SSMTP “From” Name When Sending Emails as Root User


1 views

When using SSMTP to send system emails from the root user, many administrators encounter the same issue - the sender name stubbornly remains as "root" despite attempts to modify it through standard configuration. Let's explore several practical solutions to override this behavior while maintaining root privileges.

The default behavior occurs because SSMTP typically uses the system user's name when constructing the From header. Even when specifying alternative names in the command, the mailer tends to revert to the authenticated user's identity.

The most reliable approach is to modify SSMTP's configuration file:

# Edit /etc/ssmtp/ssmtp.conf
root=yourdesiredname@yourdomain.com
maildomain=yourdomain.com
FromLineOverride=YES

For one-time sends, you can use these command variations:

echo -e "From: \"Admin Server\" <notifications@example.com>\nSubject: Test\n\nBody" | \
ssmtp -v recipient@domain.com

Create a reusable shell script for consistent sender naming:

#!/bin/bash
SENDER_NAME="System Monitor"
SENDER_EMAIL="monitor@example.com"
RECIPIENT="$1"
SUBJECT="$2"
BODY="$3"

(echo "From: \"$SENDER_NAME\" <$SENDER_EMAIL>";
 echo "Subject: $SUBJECT";
 echo "";
 echo "$BODY") | ssmtp "$RECIPIENT"

Be cautious when modifying headers - improper formatting can trigger spam filters. Always test with verbose mode (-v) first and verify the complete headers in received messages.

If flexibility is crucial, consider msmtp as an alternative with more header control:

account default
host smtp.example.com
port 587
auth on
user notifications@example.com
from "Admin Alerts <notifications@example.com>"
password yourpassword

When using SSMTP to send emails from a Linux system, you might notice that the sender name always defaults to "root" when sending as the root user. This can be problematic for several reasons:

  • Professional emails shouldn't come from "root"
  • Recipient mail servers might flag such emails as suspicious
  • You might want to use a more descriptive sender name

Many users try to override the From header by piping text to ssmtp:

echo 'From: "New name" <xy@com>' | ssmtp to@gmail.com -v

However, this approach often fails because:

  1. SSMTP still prepends its own From header
  2. The mail server might reject malformed headers
  3. The configuration file takes precedence

To properly change the sender name without creating another user, you need to modify the SSMTP configuration file:

# Edit /etc/ssmtp/ssmtp.conf
FromLineOverride=YES
maildomain=yourdomain.com
rewriteDomain=yourdomain.com
hostname=yourhostname
From: "Your Desired Name" <your@email.com>

For more control over the email headers, consider these additional settings:

# Additional headers in ssmtp.conf
AuthUser=your@email.com
AuthPass=yourpassword
AuthMethod=LOGIN
UseTLS=YES
UseSTARTTLS=YES

After making changes, test your setup with:

echo "Test message" | ssmtp -v recipient@example.com

Check the output for the correct From header. If you still see "root", verify that:

  • The config file permissions are correct (usually root:root 644)
  • You've restarted any relevant services
  • There are no conflicting settings elsewhere

If you can't modify the main configuration, create a wrapper script:

#!/bin/bash
/usr/sbin/ssmtp -C /path/to/alternate.conf "$@"

Then create alternate.conf with your custom From settings.