Simplest local mail with mailutils

I’ve come a long way in a short time setting up my nixos system, but I seem to have come unstuck with what would seem to be a simple step: setting up a minimal local-only mail (using mail from mailutils to just send/receive old-school mail between local users).

I’ve installed mailutils and figured that I’d probably have to install postfix. I’ve tried to cajole it to play ball with various settings, but so far I have no even managed to use mail to send a message to myself. There’s no error on the attempted send, but used ‘mail’ again to check my mailbox constantly says I have no mail.

Postfix might be nice if I want to grow this config to actually send and receive mail beyond the local server, but right now I would be very happy with just a local config (and so I can get results from at/batch etc. in the classic way). What would be the simplest configuration to achieve this in 20.09? Thanks.

There are .forward file (postfix) which means local delivery. So if you assign each user an email you should be done with some lines of code with postfix eventually. But seriously everybody has its own mail …

php_sendmail_to_imap = pkgs.writeScriptBin "sendmail" ''
#!${pkgs.php_marc.php8x}/bin/php
<?php
$message = file_get_contents('php://stdin');
$server = "webmail.your-server.de";

$stream = imap_open("{{$server}}INBOX", "mail", "PASS");
try {
  $check = imap_check($stream);
  imap_append($stream, "{{$server}}INBOX", $message);
  $check = imap_check($stream);
} finally {
  imap_close($stream);
}
'';

something like this is a simple sendmail command using PHP to send mails to a preconfigured account. However user/pass is plain text in store so maybe it’s not the best option. You could use secruity.wrappers and put such script at root.

But seriously., is it the best problem to spend time on ?

~> cat postfix.nix                                                                                                                                                                                                                                         
{ config, pkgs, lib, ... }:
{
  environment.systemPackages = [ pkgs.mailutils ];

  services.postfix.enable = true;

  users.users.aanderse.isNormalUser = true;
  users.users.skywalker.isNormalUser = true;
}

~> sudo nixos-container create postfix --config-file postfix.nix
~> sudo nixos-container start postfix
~> sudo nixos-container root-login postfix                                                                                                                                                                                                                 

[root@postfix:~]# su - aanderse 

[aanderse@postfix:~]$ mail -s "blah blah test" skywalker@localhost
Cc: 
this is a test
 
 
[aanderse@postfix:~]$ exit
logout

[root@postfix:~]# journalctl -u postfix.service 
-- Logs begin at Wed 2021-04-14 18:32:49 EDT, end at Wed 2021-04-14 18:33:10 EDT. --
Apr 14 18:32:50 postfix systemd[1]: Starting Postfix mail server...
Apr 14 18:32:51 postfix postfix/master[652]: daemon started -- version 3.5.6, configuration /etc/postfix
Apr 14 18:32:51 postfix systemd[1]: Started Postfix mail server.
Apr 14 18:33:09 postfix postfix/pickup[653]: 196CC7695F3: uid=1000 from=<aanderse@postfix>
Apr 14 18:33:09 postfix postfix/cleanup[672]: 196CC7695F3: message-id=<20210414223309.196CC7695F3@postfix.localdomain>
Apr 14 18:33:09 postfix postfix/qmgr[654]: 196CC7695F3: from=<aanderse@postfix>, size=357, nrcpt=1 (queue active)
Apr 14 18:33:09 postfix postfix/local[674]: 196CC7695F3: to=<skywalker@localhost>, relay=local, delay=0.01, delays=0.01/0/0/0, dsn=2.0.0, status=sent (delivered to maildir)
Apr 14 18:33:09 postfix postfix/qmgr[654]: 196CC7695F3: removed

[root@postfix:~]# su - skywalker 

[skywalker@postfix:~]$ mail
"/var/mail/skywalker": 1 message 1 new
>N   1 aanderse@postfix                     14/447   blah blah test
? 1
Return-Path: <aanderse@postfix>
X-Original-To: skywalker@localhost
Delivered-To: skywalker@localhost
Received: by postfix.localdomain (Postfix, from userid 1000)
        id 196CC7695F3; Wed, 14 Apr 2021 18:33:09 -0400 (EDT)
Subject: blah blah test
To: <skywalker@localhost>
X-Mailer: mail (GNU Mailutils 3.9)
Message-Id: <20210414223309.196CC7695F3@postfix.localdomain>
Date: Wed, 14 Apr 2021 18:33:09 -0400 (EDT)
From: aanderse@postfix

this is a test

? 

Please let me know if I can provide any clarification, context, or additional help.

5 Likes

Thanks!

I found the main problem was that somehow /var/spool/mail/luke had been created as a file somehow and not as a directory. This probably happened when I was messing with various mail options, but of course directory structures like this are not directly controlled but choices in the declarative system config, so old state can persist and mess up with new configurations.

I do still have a minor problem, that ‘mail luke@localhost’ works, but ‘mail luke’ does not. Apparently (at least by default) a missing domain in the addressee is not interpreted as localhost. I’ll see if I can find a fix for this (presumably a postfix setting, or actually requiring an MX record for myhost, even though it is clearly resolvable for IP address, e.g. ping myhost).

The log says:

Apr 14 23:26:10 myhost postfix/pickup[86144]: 7337CE5DD6: uid=1000 from=<luke@myhost>
Apr 14 23:26:10 myhost postfix/cleanup[91695]: 7337CE5DD6: message-id=<20210415062610.7337CE5DD6@myhost.localdomain>
Apr 14 23:26:10 myhost postfix/qmgr[60563]: 7337CE5DD6: from=<luke@myhost>, size=342, nrcpt=1 (queue active)
Apr 14 23:26:10 myhost postfix/smtp[91713]: 7337CE5DD6: to=<luke@myhost>, relay=none, delay=0.09, delays=0.03/0.02/0.04/0, dsn=4.4.3, status=deferred (Host or domain name not found. Name service error for name=myhost type=MX: Host not found, try again)

Thanks so much for this. It’s the kind of thing that’s so simple that people often won’t bother to write it up anywhere. But as a complete novice when it comes to mail servers, and a near-beginner with Nix itself, seeing a full working example was invaluable!

1 Like