Send email from a command line

Print

gnome terminal

Q. I would like to send an email from a command line in bash, how do I do that?

A. Assuming you have your outgoing server configured and either bsd-mailx or mailx (simple mail user agent for Linux) you can use this command to send out email:

echo "Email body text" | mail -s "Email subject" email@example.com

If you do not have your tools configured, you can send an email using telnet:

telnet localhost 25

The response should be:

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 servername.com ESMTP Postfix (Ubuntu)

Next, type in:

HELO local.servername.com

The response should be something like that:

250 servername.com

Next, type in the from email address:

mail from: fromemail.address@example.com

And you should getting something like this from the server:

250 2.1.0 Ok

Type in the recipient:

rcpt to: toemail.address@example.com

The response from the server will be along those lines:

250 2.1.5 Ok

To enter the email body, type in DATA. To finish your email body, type a dot on an empty line and press enter:

data

This is the response you'll get:

354 End data with <CR><LF>.<CR><LF>

Type in your email body:

Subject: test
Testing email.
.

Finally, if all goes well you'll get a response from the server, with something like this:

250 2.0.0 Ok: queued as 9470323291

There are many things that can go wrong while sending through telnet, such as required authentication, server misconfiguration, etc., so we assume that you have the server configured.

 

Linux