Jump to content

Postfix for Gmail: Difference between revisions

From NixOS Wiki
Klinger (talk | contribs)
mNo edit summary
Tie-ling (talk | contribs)
msmtp
 
(3 intermediate revisions by 3 users not shown)
Line 1: Line 1:
This is how to setup Postfix to use Gmail as a relay host, so it can send email via Gmail, e.g. for output of cronjobs etc.
This is how to setup Postfix to use Gmail as a relay host, so it can send email via Gmail, e.g. for output of cronjobs etc.


== Setup sops-nix ==
== Option: use msmtp instead of postfix ==
Msmtp seems to be easier to configure, see [[ZFS#Mail_notifications_(ZFS_Event_Daemon)]]


Follow https://github.com/Mic92/sops-nix
== Secrets Configuration ==


Make sure you can create secrets in /run/secrets before continuing.
This page follows the configuration defined in <code>[https://github.com/Mic92/sops-nix sops-nix]</code>, but you can use any [[Comparison of secret managing schemes|secret managing scheme]].


== Add a "postfix/sasl_passwd" secret ==
== Add a "postfix/sasl_passwd" secret ==
Line 35: Line 36:
       # optional: Forward mails to root (e.g. from cron jobs, smartd)
       # optional: Forward mails to root (e.g. from cron jobs, smartd)
       # to me privately and to my work email:
       # to me privately and to my work email:
       virtual_alias_maps = "inline:{ {root=you@gmail.com, you@work.com} }";
       virtual_alias_maps = "inline:{ {root=you@work.example, you@home.example} }";
     };
     };
   };
   };
Line 53: Line 54:
     'echo "This is a test email." | mail -s "Test Email from NixOS to root" root'
     'echo "This is a test email." | mail -s "Test Email from NixOS to root" root'


== Source / Credit ==
== References ==


This post came out of this [https://discourse.nixos.org/t/porting-my-postfix-gmail-smtp-to-nixos/30286 discussion].
[https://discourse.nixos.org/t/porting-my-postfix-gmail-smtp-to-nixos/30286 Porting my postfix gmail smtp to nixos] on the NixOS Discourse.




[[Category:Mail servers]]
[[Category:Mail Server]]
[[Category:Server]]
[[Category:Server]]

Latest revision as of 21:44, 18 September 2025

This is how to setup Postfix to use Gmail as a relay host, so it can send email via Gmail, e.g. for output of cronjobs etc.

Option: use msmtp instead of postfix

Msmtp seems to be easier to configure, see ZFS#Mail_notifications_(ZFS_Event_Daemon)

Secrets Configuration

This page follows the configuration defined in sops-nix, but you can use any secret managing scheme.

Add a "postfix/sasl_passwd" secret

Create an app password specifically for this postfix installation at https://myaccount.google.com/apppasswords (logged in as you).

You end up with a 16 character string separated by spaces. Put that in a sops secret:

 nix-shell -p sops --run "sops /etc/nixos/sops-secrets.yaml"

Create this entry, using the 16 character string without spaces (Don't use the "abcdefghjklmnopq" string below. It won't work :-) ):

 postfix:
     sasl_passwd: '[smtp.gmail.com]:587 you@gmail.com:abcdefghjklmnopq'

Configure Postfix

 sops.secrets."postfix/sasl_passwd".owner = config.services.postfix.user;
 services.postfix = {
   enable = true;
   relayHost = "smtp.gmail.com";
   relayPort = 587;
   config = {
     smtp_use_tls = "yes";
     smtp_sasl_auth_enable = "yes";
     smtp_sasl_security_options = "";
     smtp_sasl_password_maps = "texthash:${config.sops.secrets."postfix/sasl_passwd".path}";
     # optional: Forward mails to root (e.g. from cron jobs, smartd)
     # to me privately and to my work email:
     virtual_alias_maps = "inline:{ {root=you@work.example, you@home.example} }";
   };
 };

Profit

 sudo nixos-rebuild switch

Send an email explicitly to you@gmail.com

 nix-shell -p mailutils --run \
   'echo "This is a test email." | mail -s "Test Email from NixOS" you@gmail.com'

Send an email explicitly to root that gets sent to you@gmail.com and you@work.com (if you opted in for virtual_alias_maps)

 nix-shell -p mailutils --run \
   'echo "This is a test email." | mail -s "Test Email from NixOS to root" root'

References

Porting my postfix gmail smtp to nixos on the NixOS Discourse.