Problem
How to send emails with msmtp cli using outMail?
Solution
Follow the guide below to send emails using the command line tool msmtp for linux. More detailed information on msmtp usage and parameters can be found at marlam.de/msmtp [Disclaimer - 3rd party link]
This article already assumes your Linux system is working, has suitable Internet access, and you have the necessary permissions to run applications at the shell prompt. It is also assumed you have installed the msmtp application or you have built it from the source github.com/marlam/msmtp [Disclaimer - 3rd party link].
Although this KB article has been written using linux as the OS, msmtp works with Linux, MacOS and WSL (Windows Subsystem for Linux). The guide below can easily be adapted for those operating systems.
You will also need your outMail SMTP credentials.
Method 1
msmtp is a command line tool and for this example we are going to use the following command line switches
Parameters | Value |
---|---|
--from | The From Address (fred@example.com) |
--domain | Specify the domain (example.com) for the SMTP HELO command |
--host | The address of the outMail server (mxXXXXXX.smtp-engine.com) |
--port | If you wish to use an alternative port other than SMTP TCP 25 (See your welcome email for available alternative ports) |
--auth | We need to specifiy the Auth Method as LOGIN |
--user | Your outMail SMTP username |
--passwordeval | Your outMail SMTP password. msmtp requires the password be evaluated during smtp auth. |
--tls | We will use TLS in this example. However /TLS and /SSL are supported with outMail sending |
--tls-certcheck | For speed we will tell msmtp to not check the SMTP SSL certificate. |
The example command line will look like the below
printf "To: fred@example.com\nFrom: me@example.com\nSubject: My Subject line...\n\nThe body of the message...\n" | \
msmtp --host=mxXXXXXX.smtp-engine.com \
--port=8025 \
--auth=login \
--user="outmail-username" \
--passwordeval='echo outmail-password' \
--from=me@example.com \
--domain=example.com \
--tls=on \
--tls-certcheck=off \
--logfile=/tmp/msmtp.log \
fred@example.com
To check the result of the email send please check the log file, in this example its C:\SwithMail\log.txt.
Example of the log is below
MMM DD HH:MM:SS host=mxXXXXXX.smtp-engine.com tls=on auth=on user=outmail-username from=me@example.com recipients=fred@example.com mailsize=234 smtpstatus=250 smtpmsg='250 2.0.0 Ok: queued as ABC456789' exitcode=EX_OK
Method 2
The second solution is rather than specifying all the parameters on the command line you can save the settings in a profile file to use.
Let's create a profile (Note: in msmtp it's called an account) called "outmail"
Create/Edit a file called ~/.msmtprc
defaults
auth on
tls on
tls_certcheck off
logfile /tmp/msmtp.log
account outmail
host mxXXXXXX.smtp-engine.com
port 2525
from me@example.com
user outmail-username
password outmail-password
account default : outmail
The msmtp command line tool will now be used with the settings in the msmtprc file and the following command line switches
Parameters | Value |
---|---|
--account | The name of the profile (outmail) |
The example command line will look like the below
printf "To: fred@example.com\nFrom: me@example.com\nSubject: My Subject line...\n\nThe body of the message...\n" | \
msmtp --account=outmail \
fred@example.com
To check the result of the email send please check the log file, in this example its /tmp/msmtplog.
Example of the log is below
MMM DD HH:MM:SS host=mxXXXXXX.smtp-engine.com tls=on auth=on user=outmail-username from=me@example.com recipients=fred@example.com mailsize=230 smtpstatus=250 smtpmsg='250 2.0.0 Ok: queued as ABC456791' exitcode=EX_OK
Sending emails with msmtp using a shell script
The example below shows how to send emails using msmtp with outMail as the SMTP relay host in a bash shell script.
create a file called demo-msmtp-with-outmail.sh
#!/bin/bash
# Email details
TO="fred@example.com"
FROM="me@example.com"
SUBJECT="My Subject line"
BODY="The body of the message"
SMTP_SERVER="mxXXXXXX.smtp-engine.com"
SMTP_PORT="2525"
SMTP_USER="outmail-username"
SMTP_PASS="outmail-password"
# Construct the email content
EMAIL_CONTENT="To: ${TO}
From: ${FROM}
Subject: ${SUBJECT}
${BODY}"
# Send the email using msmtp
printf "${EMAIL_CONTENT}" | msmtp \
--host="${SMTP_SERVER}" \
--port="${SMTP_PORT}" \
--auth=plain \
--user="${SMTP_USER}" \
--passwordeval="echo ${SMTP_PASS}" \
--from="${FROM}" \
--tls=on \
--tls-certcheck=off \
"${TO}"
Problem Solving
Below is a list of common gotchas.
Relay access denied
You need to check you're passing the correct parameters. Remember outMail is a SMTP authenticated service. Double check your outMail SMTP credentials (username and password). Both are case sensitive.
Parameters | Value |
---|---|
--host | The address of the outMail server (mxXXXXXX.smtp-engine.com) |
--username | Your outMail username |
--passwordeval | Your outMail password. Use the passwordeval parameter e.g. --passwordeval="echo outmail-password" |
Summary of server details
Outgoing server |
mxXXXXXX.smtp-engine.com As provided in your signup email. |
Outgoing server protocol |
SMTP |
Outgoing server port |
25, 465, 587, 2525 or 8025 |
Authentication Type |
Basic Authentication, SSL and TLS supported |
Username |
As provided |
Password |
As provided |