Jump to content

Msmtp: Difference between revisions

From NixOS Wiki
msmtp includes the msmtp-queue script to queue offline messages.
Tie-ling (talk | contribs)
Configuration: merged from zfs article
 
Line 36: Line 36:


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
programs.msmtp = {
{
   enable = true;
  age.secrets.msmtp = {
   accounts = {
    file = "${inputs.self.outPath}/secrets/msmtp.age";
     default = {
  };
       auth = true;
 
       tls = true;
   # for zed enableMail, enable sendmailSetuidWrapper
       # try setting `tls_starttls` to `false` if sendmail hangs
  services.mail.sendmailSetuidWrapper.enable = true;
       from = "<from address here>";
 
       host = "<hostname here>";
   programs.msmtp = {
       user = "<username here>";
     enable = true;
      passwordeval = "cat /secrets/smtp_password.txt";
    setSendmail = true;
    defaults = {
       aliases = "/etc/aliases";
       port = 587;
       auth = "plain";
       tls = "on";
       tls_starttls = "on";
    };
    accounts = {
       default = {
        host = "smtp.mail.example.com";
        passwordeval = "cat ${config.age.secrets.msmtp.path}";
        user = "myname@example.com";
        from = "myname@example.com";
      };
     };
     };
   };
   };
};
}
</syntaxhighlight>
</syntaxhighlight>


Line 56: Line 70:
=== Aliases ===
=== Aliases ===


Example:
Then, configure an alias for root account. With this alias configured, all mails sent to root, such as cron job results and failed sudo login events, will be redirected to the configured email account.


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
programs.msmtp.defaults = {
{
   aliases = "/etc/aliases";
   environment.etc.aliases.text = ''
};
    root: admin@example.com
 
  '';
environment.etc = {
}
  "aliases" = {
    text = ''
      root: me@example.com
    '';
    mode = "0644";
  };
};
</syntaxhighlight>
</syntaxhighlight>



Latest revision as of 10:02, 6 October 2025

msmtp is an easy to configure basic email sender client with fairly complete sendmail compatibility.

Installation

A minimal configuration to relay mails through an external mail server coud look like this

⚠︎
Warning: Do not use a plaintext password in production, this is for demonstration only
programs.msmtp = {
  enable = true;
  accounts.default = {
    host = "example.org";
    from = "hello@example.org";
    user = "hello@example.org";
    password = "mypassword123";
  };
};

In this case msmtp will try to deliver mails through the smtp server example.org on port 25. user and password are used for normal plaintext authentication.

This configuration will automatically set msmtp as the default mail delivery client on your system by overwriting the sendmail binary. To test mail delivery issue following command:

# echo -e "Content-Type: text/plain\r\nSubject: Test\r\n\r\nHello World" | sendmail john.doe@mail.com

A mail with the subject Test will be sent to the recipient john.doe@mail.com including the body text Hello World. A Mime-Header is added to the mail content for better compatibility.

Configuration

Further configuration options for msmtp can be found here.

TLS connections

{
  age.secrets.msmtp = {
    file = "${inputs.self.outPath}/secrets/msmtp.age";
  };

  # for zed enableMail, enable sendmailSetuidWrapper
  services.mail.sendmailSetuidWrapper.enable = true;

  programs.msmtp = {
    enable = true;
    setSendmail = true;
    defaults = {
      aliases = "/etc/aliases";
      port = 587;
      auth = "plain";
      tls = "on";
      tls_starttls = "on";
    };
    accounts = {
      default = {
        host = "smtp.mail.example.com";
        passwordeval = "cat ${config.age.secrets.msmtp.path}";
        user = "myname@example.com";
        from = "myname@example.com";
      };
    };
  };
}

Note that msmtp has no daemon and runs as the invoking user. If using passwordeval, the file must be readable by any user that wishes to send mail.

Aliases

Then, configure an alias for root account. With this alias configured, all mails sent to root, such as cron job results and failed sudo login events, will be redirected to the configured email account.

{
  environment.etc.aliases.text = ''
    root: admin@example.com
  '';
}

See also