<- Back

# Setup your own mail paste service

Last modification on 2024-02-10

## How it works

* The user sends a mail with an attachment to a certain mail address, for
  example: paste@somehost.org
* The mail daemon configuration has an mail alias to pipe the raw mail to a
  shellscript.
* This shellscript processes the raw mail contents from stdin.


## What it does

* Process a mail with the attachments automatically.
* The script processes the attachments in the mail and stores them.
* It will mail (back) the URL where the file(s) are stored.

This script is tested on OpenBSD using OpenBSD smtpd and OpenBSD httpd and the
gopher daemon geomyidae.


## Install dependencies

On OpenBSD:

        pkg_add mblaze


## smtpd mail configuration

In your mail aliases (for example /etc/mail/aliases) put:

        paste: |/usr/local/bin/paste-mail


This pipes the mail to the script paste-mail for processing, this script is
described below. Copy the below contents in /usr/local/bin/paste-mail


Script:

        #!/bin/sh
        
        d="/home/www/domains/www.codemadness.org/htdocs/mailpaste"
        tmpmsg=$(mktemp)
        tmpmail=$(mktemp)
        
        cleanup() {
                rm -f "$tmpmail" "$tmpmsg"
        }
        
        # store whole mail from stdin temporarily, on exit remove temporary file.
        trap "cleanup" EXIT
        cat > "$tmpmail"
        
        # mblaze: don't store mail sequence.
        MAILSEQ=/dev/null
        export MAILSEQ
        
        # get from address (without display name).
        from=$(maddr -a -h 'From' /dev/stdin < "$tmpmail")
        
        # check if allowed or not.
        case "$from" in
        "hiltjo@codemadness.org")
                ;;
        *)
                exit 0;;
        esac
        
        # prevent mail loop.
        if printf '%s' "$from" | grep -q "paste@"; then
                exit 0
        fi
        
        echo "Thank you for using the enterprise paste service." > "$tmpmsg"
        echo "" >> "$tmpmsg"
        echo "Your file(s) are available at:" >> "$tmpmsg"
        echo "" >> "$tmpmsg"
        
        # process each attachment.
        mshow -n -q -t /dev/stdin < "$tmpmail" | sed -nE 's@.*name="(.*)".*@\1@p' | while read -r name; do
                test "$name" = "" && continue
        
                # extract attachment.
                tmpfile=$(mktemp -p "$d" XXXXXXXXXXXX)
                mshow -n -O /dev/stdin "$name" < "$tmpmail" > "$tmpfile"
        
                # use file extension.
                ext="${name##*/}"
                case "$ext" in
                *.tar.*)
                        # special case: support .tar.gz, tar.bz2, etc.
                        ext="tar.${ext##*.}";;
                *.*)
                        ext="${ext##*.}";;
                *)
                        ext="";;
                esac
                ext="${ext%%*.}"
        
                # use file extension if it is set.
                outputfile="$tmpfile"
                if test "$ext" != ""; then
                        outputfile="$tmpfile.$ext"
                fi
                mv "$tmpfile" "$outputfile"
                b=$(basename "$outputfile")
        
                chmod 666 "$outputfile"
                url="gopher://codemadness.org/9/mailpaste/$b"
        
                echo "$name:" >> "$tmpmsg"
                echo "        Text   file: gopher://codemadness.org/0/mailpaste/$b" >> "$tmpmsg"
                echo "        Image  file: gopher://codemadness.org/I/mailpaste/$b" >> "$tmpmsg"
                echo "        Binary file: gopher://codemadness.org/9/mailpaste/$b" >> "$tmpmsg"
                echo "" >> "$tmpmsg"
        done
        
        echo "" >> "$tmpmsg"
        echo "Sincerely," >> "$tmpmsg"
        echo "Your friendly paste_bot" >> "$tmpmsg"
        
        # mail back the user.
        mail -r "$from" -s "Your files" "$from" < "$tmpmsg"
        
        cleanup


The mail daemon processing the mail needs of course to be able to have
permissions to write to the specified directory. The user who received the mail
needs to be able to read it from a location they can access and have
permissions for it also.


## Room for improvements

This is just an example script. There is room for many improvements.
Feel free to change it in any way you like.


## References
* https://man.openbsd.org/aliases
* https://man.openbsd.org/smtpd
* https://man.openbsd.org/httpd
* https://github.com/leahneukirchen/mblaze

## Bye bye

I hope this enterprise(tm) mail service is inspirational or something ;)