Guidelines

This site is for tech Q&A. Please keep your posts focused on the subject at hand.

Ask one question at a time. Don't conflate multiple problems into a single question.

Make sure to include all relevant information in your posts. Try to avoid linking to external sites.

Links to documentation are fine, but in addition you should also quote the relevant parts in your posts.

0 votes
295 views
295 views

For testing a new mail server (Postfix) I need to send an email locally, preferably from the commandline. I know I can use netcat or telnet to talk to the SMTP service directly

me@server:~ $ telnet localhost 25
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 foo.example.com ESMTP Postfix
helo example.org
250 foo.example.com
mail from:<me@example.org>
250 2.1.0 Ok
rcpt to:<postmaster@foo.example.com>
250 2.1.5 Ok
data
354 End data with .
Subject: test

test
.
250 2.0.0 Ok: queued as A103C452
quit
221 2.0.0 Bye
Connection closed by foreign host.

but isn't there a more convenient way?

in Sysadmin
by (115)
2 19 33
edit history

Please log in or register to answer this question.

1 Answer

0 votes
 

There are several ways, actually.

The simplest approach is most likely the sendmail command that ships with Postfix, since that does not require additional software:

from='me@example.org'
to='postmaster@foo.example.com'
subj='Foo'
msg='Your message.'

echo -e "Subject: ${subj}\n\n${msg}" | sendmail -f "$from" "$to"

Another option would be the mailx commandline utility. However, you probably need to install that first (for Debian-based systems it's in the package bsd-mailx):

from='me@example.org'
to='postmaster@foo.example.com'
subj='Foo'
msg='Your message.'

echo "$msg" | mailx -r "$from" -c '' -s "$subj" "$to"

Or you could install something like Mutt, which provides you with an ncurses-based interface. The program needs some configuration, though. Create a file .muttrc with the following content in your userhome:

# ~/.muttrc
set mbox_type=maildir
set spoolfile="/var/mail/USERNAME"
set folder="~/Mail/"

Replace USERNAME with your actual username on the system.

You can instruct the client to use a specific from address by adding this line:

set from=me@example.org

and/or make headers (including the From: header) editable by adding

set edit_hdrs=yes

edited by
by (115)
2 19 33
edit history
...