
How to Use the Command 'msmtp' (with examples)
msmtp is a lightweight SMTP client that allows users to send email messages from the command line. It reads text input from stdin
and forwards it to a specified SMTP server. This tool is particularly useful for automating email notifications in scripts or applications. It supports multiple accounts configured in a configuration file typically found at ~/.msmtprc
.
Use case 1: Send an email using the default account configured in ~/.msmtprc
Code:
echo "Hello world" | msmtp to@example.org
Motivation:
This use case is simple yet powerful for those who want to quickly send an email without specifying multiple parameters each time. By using msmtp with the default account, users can streamline their workflow, merely needing to supply the recipient’s address. This is particularly useful for users who frequently send emails to a select set of recipients or who have automated scripts that generate email content dynamically.
Explanation:
echo "Hello world"
: This part of the command prints the string “Hello world” to standard output, which msmtp reads as the message body.|
: The pipe operator directs the output of theecho
command intomsmtp
.msmtp
: This is the command for the SMTP client, which is configured in the user’s home directory under~/.msmtprc
.to@example.org
: This argument specifies the recipient’s email address.
Example output:
There would be no output to the terminal upon successful execution, but the recipient would receive an email with “Hello world” as its content.
Use case 2: Send an email using a specific account configured in ~/.msmtprc
Code:
echo "Hello world" | msmtp --account=account_name to@example.org
Motivation:
This use case is designed for users managing multiple email accounts or domains. For instance, businesses often communicate with clients through different addresses for support, sales, and general inquiries. Specifying an account allows the user to select which configured account they wish to send the email from, facilitating precise and organized communications.
Explanation:
echo "Hello world"
: Generates the message content to be sent.|
: Pipes the output to msmtp.msmtp --account=account_name
: The--account
flag tells msmtp to use a specific account identified in the~/.msmtprc
file.account_name
is a placeholder for the actual account label.to@example.org
: The recipient’s email address.
Example output:
Similar to the first case, successful execution will not provide terminal output, but the specified recipient will receive the email from the chosen account.
Use case 3: Send an email without a configured account
Code:
echo "Hello world" | msmtp --host=localhost --port=999 --from=from@example.org to@example.org
Motivation:
This use case suits scenarios where a user needs to send emails without permanent configuration, such as temporary setups or dynamic environments where SMTP account credentials cannot be stored. It allows flexibility in defining SMTP server parameters on the fly, useful for testing or unique server configurations.
Explanation:
echo "Hello world"
: Creates the message content.|
: Directs this content into msmtp.msmtp --host=localhost --port=999 --from=from@example.org
: These flags customize the SMTP server details directly in the command line.--host=localhost
specifies the SMTP server’s address.--port=999
specifies the SMTP server port to connect to.--from=from@example.org
sets the sender’s email address.
to@example.org
: Defines the email recipient.
Example output:
There would be no visible output on the terminal upon successful email sending, but the recipient will get an email from ‘from@example.org ’, routed through the specified SMTP server.
Conclusion:
Using msmtp is a straightforward method to send emails via an SMTP server from the command line. It provides flexibility through configuration in the ~/.msmtprc
file and can be tailored for different accounts or even without an account setup. This versatility makes it an essential tool for script-based automation and controlled email sending.