How to use the command msmtp (with examples)
Description:
The msmtp
command is an SMTP client that allows you to send emails by reading text from stdin
and sending it to an SMTP server. It is commonly used as a lightweight alternative to full-fledged mail transfer agents (MTAs).
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 useful when you have a default account configured in the ~/.msmtprc
file and you want to quickly send an email without specifying any additional settings. It enables a streamlined workflow for sending emails from the command line.
Explanation:
In this use case, the echo "Hello world"
command is piped to msmtp
, which reads the text from stdin
. The to@example.org
argument specifies the email address of the recipient.
Example Output:
The email with the subject “Hello world” is sent to the to@example.org
email address using the default account configured in ~/.msmtprc
.
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:
If you have multiple accounts configured in the ~/.msmtprc
file and want to select a specific account to send the email, this use case is helpful. It allows you to differentiate between email accounts and ensures that the email is sent using the desired account.
Explanation:
Similar to the previous use case, the echo "Hello world"
command is piped to msmtp
. The --account=account_name
argument specifies the name of the account you want to use from the ~/.msmtprc
file. The to@example.org
argument specifies the email address of the recipient.
Example Output:
The email with the subject “Hello world” is sent to the to@example.org
email address using the specified account (account_name
) configured in ~/.msmtprc
.
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 is useful when you don’t have a pre-configured account in the ~/.msmtprc
file, but still want to send an email. It allows you to specify all the necessary SMTP settings directly in the command, including the host, port, and sender’s email address.
Explanation:
In this use case, the echo "Hello world"
command is piped to msmtp
. The --host=localhost
argument specifies the hostname of the SMTP server to use. The --port=999
argument specifies the port number on which the SMTP server is running. The --from=from@example.org
argument specifies the email address of the sender, while the to@example.org
argument specifies the email address of the recipient.
Example Output:
The email with the subject “Hello world” is sent to the to@example.org
email address using the specified SMTP settings (localhost:999
) and the provided sender’s email address (from@example.org
).
Conclusion:
The msmtp
command is a versatile tool for sending emails from the command line. It allows you to send emails using different accounts, as well as specify custom SMTP settings when needed. By understanding and utilizing the various use cases illustrated above, you can efficiently send emails directly from the terminal.