Shell Script to .muttrc – How to Pass a Variable

bashmuttshell-script

I'd like mutt (mutt-kz, actually — I'd love to migrate to neomutt but that's a bigger project) to get my SMTP password from pass so I don't have to keep it stored in cleartext in my config file, but I'm not sure how to properly pass the password into my .muttrc.

This is what I have in .muttrc:

source ~/.mutt/pass.sh|
set smtp_pass = $my_pass

And then .mutt/pass.sh contains:

#! /bin/bash
my_pass=$(pass Example/user)

If I add echo ${my_pass} to pass.sh and run it from the command line, it does echo my password.

When I launch mutt-kz I'm prompted to unlock my pass gpg key, so something is working, but when I try to send mail from Mutt it asks for my SMTP password and fails if I don't provide it the message isn't sent: ("SASL authentication failed")

So … how do I correctly pass the variable from my shell script to my .muttrc?

Best Answer

The program ~/.mutt/pass.sh must produce output that looks like

set my_pass = swordfish

So it can be something like

#!/bin/sh
echo "set my_pass = swordfish"

Note that if your password contains certain special characters (\'" and whitespace), you'll need to quote it. For example, if your password is in the text file ~/passwords/smtp.txt, you can use:

#!/bin/sh
~/passwords/smtp.txt sed 's/["$`\\]/\\&/g; s/^/set my_pass = "/; s/$/"/'

For the password sword\fish, this prints set my_pass="sword\\fish".

If you use Gnome-keyring to store your passwords then you can use the secret-tool utility to read your password from the keyring. For example, this is what I use for an Office 365 password (with a password that doesn't contain any special character that needs quoting):

source 'printf "set imap_pass="; secret-tool lookup https://login.microsoftonline.com/common/login |'
Related Question