msmtp icon indicating copy to clipboard operation
msmtp copied to clipboard

feat: Use From: header as envelope-from

Open WhyNotHugo opened this issue 2 years ago • 7 comments

I've a very simple/basic setup to send emails from my desktop via my SMTP:

tls on
host smtp.example.com
auth on
user [email protected]
passwordeval hiq -dFpassword proto=smtp hostname=smtp.example.com [email protected]
domain %h

This doesn't quite work with tools that generate and send emails (the first example that comes to mind is git send-email.

How, from what I understand, I'd need to configure a new account for each different From that I use. As far as I can tell, I can use defaults to avoid repeating the above block for each one, but I still have to edit the configuration file for each new address/alias.

I wonder what thoughts are on simply using the email address in the From: header as the envelope-from as a fallback (e.g.: if nothing else is explicitly specified), even if it's disabled by default.

WhyNotHugo avatar Apr 02 '23 13:04 WhyNotHugo

There is the --read-envelope-from option, which takes the envelope from address from the From: header.

With this option, msmtp chooses a matching account based on this envelope from address. To support more than one envelope from address in a single account definition, you can use wildcards (like in shell filename expansion) in the from command in ~/.msmtprc, for example from hugo*@example.com or from *[email protected].

marlam avatar Apr 22 '23 16:04 marlam

There is the --read-envelope-from option, which takes the envelope from address from the From: header.

Right, what I'm looking for is a configuration flag that forces the same behavior. I don't usually control the invocation to sendmail (e.g.: it gets called by another application like git or mutt), so I can't just add a flag to the invocation.

WhyNotHugo avatar Apr 28 '23 11:04 WhyNotHugo

A workaround could be to set /usr/sbin/sendmail to something like this:

#!/bin/sh
exec /usr/bin/msmtp --read-envelope-from ... "$@"

While it is possible to add a configuration command like read_envelope_from, that would have unfortunate interactions with the account choosing mechanism, so I'm not sure if it's worth it.

marlam avatar May 02 '23 19:05 marlam

While it is possible to add a configuration command like read_envelope_from, that would have unfortunate interactions with the account choosing mechanism, so I'm not sure if it's worth it.

Wouldn't the above wrapper script have the exact same caveats?

WhyNotHugo avatar Jul 24 '23 22:07 WhyNotHugo

As an alternative approach, what do you think of the from directive having a value like from header that sets the envelope from to the same value as the From: header?

WhyNotHugo avatar Jul 24 '23 22:07 WhyNotHugo

If we added from header (or an extra command, e.g. read_envelope_from on), then this would only be useful if msmtp knows which account to use via some way other than automatic account selection (i.e. either --account or --host, or if both are unused: account default). Because the new command only applies after the account was chosen. So it would not work the same way as --read-envelope-from. That would probably become confusing.

marlam avatar Jul 25 '23 15:07 marlam

While it is possible to add a configuration command like read_envelope_from, that would have unfortunate interactions with the account choosing mechanism, so I'm not sure if it's worth it.

Wouldn't the above wrapper script have the exact same caveats?

@WhyNotHugo I think the difference is that the --read-envelope-from flag instructs msmtp to alter its default behaviour, while the configuration file only contains specifications for the accounts. That is, the configuration file has no account-independent section where this would make sense to specify.

But I do agree that what you suggest would probably make sense to default to when from is missing for the specified account, although it might be slightly tricky since msmtp already has the opposite function: add the From: header based on the envelope-from (set_from_header).

Anyway, I think for what you write your best option is currently to either use the wrapper with either added -a account or with setting the account as default in your configuration file, or to just tell the respective programs to invoke sendmail like that: I don't know about mutt but for git having

[sendemail]
    sendmailcmd = msmtp --read-envelope-from

works for me and when I want to explicitly override it, I use git send-email --sendmail-cmd="msmtp -a...".

balejk avatar Apr 10 '24 12:04 balejk

Thanks for these comments. I'll close this issue now since I see no way to add this functionality without breaking something.

marlam avatar May 01 '24 11:05 marlam