Msmtp: Difference between revisions

imported>Cyounkins
Initial creation
 
Tie-ling (talk | contribs)
Configuration: merged from zfs article
 
(12 intermediate revisions by 5 users not shown)
Line 1: Line 1:
[https://marlam.de/msmtp/ msmtp] is a basic email sender. It is easy to configure but lacks some features like queuing when offline.
[https://marlam.de/msmtp/ msmtp] is an easy to configure basic email sender client with fairly complete sendmail compatibility.


[https://search.nixos.org/options?query=programs.msmtp msmtp options list]
== Installation ==


== Basic configuration with TLS connections ==
A minimal configuration to relay mails through an external mail server coud look like this
 
{{warning|1=Do not use a plaintext password in production, this is for demonstration only}}


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
programs.msmtp = {
programs.msmtp = {
   enable = true;
   enable = true;
   accounts = {
   accounts.default = {
     default = {
     host = "example.org";
      auth = true;
    from = "hello@example.org";
      tls = true;
    user = "hello@example.org";
      from = "<from address here>";
    password = "mypassword123";
      host = "<hostname here>";
      user = "<username here>";
      passwordeval = "cat /secrets/smtp_password.txt";
    };
   };
   };
};
};
</syntaxhighlight>
</syntaxhighlight>


Note that msmtp has no daemon and runs as the invoking user. If using <code>passwordeval</code>, the file must be readable by any user that wishes to send mail.
In this case ''msmtp'' will try to deliver mails through the smtp server <code>example.org</code> on port <code>25</code>. <code>user</code> and <code>password</code> are used for normal plaintext authentication.
 
This configuration will automatically set ''msmtp'' as the default mail delivery client on your system by overwriting the <code>sendmail</code> binary. To test mail delivery issue following command:
 
<syntaxhighlight lang="console">
# echo -e "Content-Type: text/plain\r\nSubject: Test\r\n\r\nHello World" | sendmail john.doe@mail.com
</syntaxhighlight>
 
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 ==


== Aliases ==
Further configuration options for ''msmtp'' can be found [https://marlam.de/msmtp/msmtp.html here].


Adding <code>extraConfig</code> and creating <code>/etc/aliases</code> may be needed for aliases to work:
=== TLS connections ===


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
programs.msmtp.extraConfig = ''
{
  aliases /etc/aliases
  age.secrets.msmtp = {
'';
    file = "${inputs.self.outPath}/secrets/msmtp.age";
  };
 
  # for zed enableMail, enable sendmailSetuidWrapper
  services.mail.sendmailSetuidWrapper.enable = true;


environment.etc = {
  programs.msmtp = {
  "aliases" = {
    enable = true;
     text = ''
    setSendmail = true;
       root: me@example.com
    defaults = {
    '';
      aliases = "/etc/aliases";
    mode = "0644";
      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>
 
Note that msmtp has no daemon and runs as the invoking user. If using <code>passwordeval</code>, 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.
 
<syntaxhighlight lang="nix">
{
  environment.etc.aliases.text = ''
    root: admin@example.com
  '';
}
</syntaxhighlight>
</syntaxhighlight>
== See also ==
* [https://search.nixos.org/options?query=programs.msmtp msmtp options list]
[[Category:Applications]]