Skip to content

Instantly share code, notes, and snippets.

@sinewalker
Last active April 14, 2023 10:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sinewalker/387a8da467c332bbb733c82100581ccf to your computer and use it in GitHub Desktop.
Save sinewalker/387a8da467c332bbb733c82100581ccf to your computer and use it in GitHub Desktop.
Sending email from Linux command line (postfix or netcat)
  • Send a blank test message with a subject to an email address:

    mail -s "Subject" user@example.com < /dev/null

    (you can substitute /dev/null with a file, or pipe, to populate the body. It must end with a period on a line by itself.)

  • Send a blank email with an attachment:

    mail -a somefile -s "Subject" user@example.com < /dev/null

  • Use netcat or telnet

mjl@tesla> nc localhost 25
220 tesla.local ESMTP Postfix
HELO gmail.com
250 tesla.local
MAIL FROM: mike@tesla.local
250 2.1.0 Ok
RCPT TO: user@example.com
250 2.1.5 Ok
DATA
354 End data with <CR><LF>.<CR><LF>
Subject: test message

hello from tesla
.
250 2.0.0 Ok: queued as 7D91946
QUIT
221 2.0.0 Bye
mjl@tesla>

The SMTP commands may be lower-case, I spelled them in uppercase here for clarity. There is no SMTP command for Subject, use the DATA command and add whatever headers you want, separate from the body with a blank line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment