+++
title = 'Virgin (Neo)Mutt User Meet Mblaze'
date = 2023-10-24T18:13:18+02:00
tags = [ 'linux', 'mail', 'mblaze' ]
draft = false
+++

We have all heard of (Neo)Mutt when it comes to managing e-mail in the terminal right? What would you do if I told you there is an even better and more UNIX way to manage mail on the terminal? Well there is and it's called [mblaze](https://github.com/leahneukirchen/mblaze).

<!--more-->

In this post I will explain to you how to setup mblaze for managing your email, but for that we will 2 more additional software to manage our mail with mblaze. [OpenSMTPD](https://www.opensmtpd.org/) the openbsd fast mail server for getting local mail from services like crontab and sending mail with, and [isync](https://isync.sourceforge.io/) for downloading our mail from our remote server's either selfhosted or gmail.

# 1. OpenSMTPD

First we will setup OpenSMTP for local mail, and we will also relay's that allow us to send e-mail from our selfhosted or gmail mail server's for example. Make sure to install opensmtpd from your package maanager.

> Edit `/etc/smtpd/smtpd.conf`

```conf
table aliases file:/etc/mail/aliases
table credentials file:/etc/mail/credentials

listen on 127.0.0.1

action "local_mail" maildir "/home/%{user.username}/.local/share/Maildir/local" alias <aliases>
action outbound_selfhost relay host smtp+tls://selfhost@subdomain.domain.com:587 auth <credentials>
action outbound_gmail relay host smtp+tls://gmail@smtp.gmail.com:587 auth <credentials>

match for local action "local_mail"
match mail-from "selfhost@domain.com" for any action outbound_selfhost
match mail-from "username@gmail.com" for any action outbound_gmail
```

Next you will need to update aliases in file /etc/mail/aliases under the comment `# Well-known aliases -- these should be filled in!` to your main user account on your system.

Now we will also setup the passwords in the `/etc/mail/credentials`, Note for gmail you will need to setup app password for it to be usable with this method and enable smtp in gmail.

```conf
selfhost selfhost:selfhost_password
gmail gmail:gmail_password
```

Also make sure to run `sudo chmod 600 /etc/mail/credentials` to make sure the password's aren't readable if you aren't running as sudo.

# 2. isync

Now we will setup isync to get our e-mail from our server's. In this example there is both settings for a selfhosted server and a gmail one.

> Edit `$HOME/.mbsyncrc`

```rc
IMAPStore selfhost-remote
Host subdomain.domain.com
Port 993
User selfhost@domain.com
PassCmd "Command for password" or Pass password
SSLType IMAPS
CertificateFile /etc/ssl/certs/ca-certificates.crt

MaildirStore selfhost-local
Path ~/.local/share/Maildir/selfhost/
Inbox ~/.local/share/Maildir/selfhost/INBOX
SubFolders Verbatim

Channel selfhost
Far :selfhost-remote:
Near :selfhost-local:
Create Both
Expunge Both
Patterns * !"[Gmail]/All Mail" !"*fts-flatcurve*" !"*virtual*"
SyncState *
Create Both

IMAPStore gmail-remote
Host imap.gmail.com
Port 993
User gmail@gmail.com
PassCmd "Command for password" or Pass password
SSLType IMAPS
CertificateFile /etc/ssl/certs/ca-certificates.crt

MaildirStore gmail-local
Path ~/.local/share/Maildir/gmail/
Inbox ~/.local/share/Maildir/gmail/INBOX
SubFolders Verbatim

Channel gmail
Far :gmail-remote:
Near :gmail-local:
Create Both
Expunge Both
Patterns * !"[Gmail]/All Mail" !"*fts-flatcurve*" !"*virtual*"
SyncState *
Create Both
```

Then you can run `mbsync -V gmail/selfhost` to sync a specific account or you can run `mbsync -a` to sync all your account's.

# 3. mblaze

Now we will start working on the most juicy part, setting up mblaze for basic usage. First we will setup a basic mblaze profile for our local account.

> Edit `$HOME/.mblaze/profile`

```conf
Local-Mailbox: user
FQDN: "Generate using command mgenmid"
Maildir: /home/USER/.local/share/Maildir/local
Outbox: /home/USER/.local/share/Maildir/local/Sent/
Drafts: /home/USER/.local/share/Maildir/local/Drafts/
Reply-From: user
# You don't need this, it just makes it easier to see date
Scan-Format: %c%u%r %-3n    %10d    %17f %t %2i%s
```

And now we are ready for using mblaze for managing our local mail with mblaze. Now for the rest of this blog I will show how I manage multiple account's using a scipt and a couple function's. I will also link a video that will show you in more detail things for using mblaze for managing your mail which was my inspiration for making this post.

# 4. Multiple profiles management

You can do this in a lot of ways with mblaze since it is very easily scriptable, but I do it with a script that copies over a preconfigure profile from `$HOME/.config/mblaze`

> example selfhost mblaze config `$HOME/.config/mblaze/selfhost`

```conf
Local-Mailbox: User Name <user@domain.com>
FQDN: "Generate using command mgenmid"
Maildir: /home/USER/.local/share/Maildir/selfhost
Outbox: /home/USER/.local/share/Maildir/selfhost/Sent/
Drafts: /home/USER/.local/share/Maildir/selfhost/Drafts/
Reply-From: User Name <user@domain.com>
Scan-Format: %c%u%r %-3n    %10d    %17f %t %2i%s
```

> example gmail mblaze config `$HOME/.config/mblaze/gmail`

```conf
Local-Mailbox: user <user@gmail.com>
FQDN: "Generate using command mgenmid"
Maildir: /home/USER/.local/share/Maildir/gmail
Outbox: /home/USER/.local/share/Maildir/gmail/[Gmail]/Sent Mail
Drafts: /home/USER/.local/share/Maildir/gmail/[Gmail]/Drafts
Reply-From: user <user@gmail.com>
Scan-Format: %c%u%r %-3n    %10d    %17f %t %2i%s
```

For the local profile, just copy the config saved to `$HOME/.mblaze/profile` to `$HOME/.config/mblaze/local`.

Now onto the script and functions I use with my zsh.

> mprofile
```bash
#!/bin/sh

profiles=$(find "$HOME"/.config/mblaze -type f -exec basename "{}" \;)
currentMaildir=$(grep "^Maildir:" "$HOME"/.mblaze/profile | cut -d: -f 2 | sed 's/ //g')

[ -z "$1" ] && basename "$(grep -w "$currentMaildir" -l -R "$HOME"/.config/mblaze)" && exit 0
[ "$1" = "-l" ] && printf '%s\n' "$profiles" && exit 0

profile="$1"

if printf '%s\n' "$profiles" | grep -qw "$profile"; then
  cp "$HOME"/.config/mblaze/"$profile" "$HOME"/.mblaze/profile
else
  printf '%s\n' "This profile doesn't exist"
fi
```

The script is able to print the current profile when run without argument, listing all available profiles using -l and setting the profile by providing it's name.

> functions
```bash
# mblaze functions

# Get new mail for current profile
function mnew () {
  maildir=$(grep "^Maildir:" $HOME/.mblaze/profile | cut -d: -f 2 | sed 's/ //g')
  profile=$(basename $maildir)
  if [ "$profile" = "local" ]; then
    mlist -s "$maildir"| msort -dr | mseq -S
  else
    mbsync -V $profile
    mlist -s "$maildir"/INBOX | msort -dr | mseq -S
    minc "$maildir/INBOX" > /dev/null
  fi
}

# Get full mail for current profile including threads
function mall () {
  maildir=$(grep "^Maildir:" $HOME/.mblaze/profile | cut -d: -f 2 | sed 's/ //g')
  sent=$(grep "^Outbox:" $HOME/.mblaze/profile | cut -d: -f 2 | sed 's/ //g')
  profile=$(basename $maildir)
  if [ "$profile" = "local" ]; then
    mlist "$maildir" | mthread -r -S "$maildir" | mseq -S
  else
    mbsync -V $profile
    mlist "$maildir"/INBOX | mthread -r -S "$sent" | mseq -S
    minc "$maildir/INBOX" > /dev/null
  fi
}
```

For the rest of usage of mblaze, I really recommend to use `man mblaze` since the software is really well documented, or you can also watch this [video](https://piped.cronyakatsuki.xyz/watch?v=5YS8RPC4zwc) I took the inspiration from for this setup.

# Conclusion

Hope you have had a good read, and I hope you will maybe try out this mail setup, or create your own even better setup for your self.