mailservice.html - www.codemadness.org - www.codemadness.org saait content files
 (HTM) git clone git://git.codemadness.org/www.codemadness.org
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       mailservice.html (5900B)
       ---
            1 <!DOCTYPE html>
            2 <html dir="ltr" lang="en">
            3 <head>
            4         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
            5         <meta http-equiv="Content-Language" content="en" />
            6         <meta name="viewport" content="width=device-width" />
            7         <meta name="keywords" content="mail, paste, service" />
            8         <meta name="description" content="Setup your own mail paste service using mblaze" />
            9         <meta name="author" content="Hiltjo" />
           10         <meta name="generator" content="Static content generated using saait: https://codemadness.org/saait.html" />
           11         <title>Setup your own mail paste service - Codemadness</title>
           12         <link rel="stylesheet" href="style.css" type="text/css" media="screen" />
           13         <link rel="stylesheet" href="print.css" type="text/css" media="print" />
           14         <link rel="alternate" href="atom.xml" type="application/atom+xml" title="Codemadness Atom Feed" />
           15         <link rel="alternate" href="atom_content.xml" type="application/atom+xml" title="Codemadness Atom Feed with content" />
           16         <link rel="icon" href="/favicon.png" type="image/png" />
           17 </head>
           18 <body>
           19         <nav id="menuwrap">
           20                 <table id="menu" width="100%" border="0">
           21                 <tr>
           22                         <td id="links" align="left">
           23                                 <a href="index.html">Blog</a> |
           24                                 <a href="/git/" title="Git repository with some of my projects">Git</a> |
           25                                 <a href="/releases/">Releases</a> |
           26                                 <a href="gopher://codemadness.org">Gopherhole</a>
           27                         </td>
           28                         <td id="links-contact" align="right">
           29                                 <span class="hidden"> | </span>
           30                                 <a href="feeds.html">Feeds</a> |
           31                                 <a href="pgp.asc">PGP</a> |
           32                                 <a href="mailto:hiltjo@AT@codemadness.DOT.org">Mail</a>
           33                         </td>
           34                 </tr>
           35                 </table>
           36         </nav>
           37         <hr class="hidden" />
           38         <main id="mainwrap">
           39                 <div id="main">
           40                         <article>
           41 <header>
           42         <h1>Setup your own mail paste service</h1>
           43         <p>
           44         <strong>Last modification on </strong> <time>2024-02-10</time>
           45         </p>
           46 </header>
           47 
           48 <h2>How it works</h2>
           49 <ul>
           50 <li>The user sends a mail with an attachment to a certain mail address, for
           51 example: paste@somehost.org</li>
           52 <li>The mail daemon configuration has an mail alias to pipe the raw mail to a
           53 shellscript.</li>
           54 <li>This shellscript processes the raw mail contents from stdin.</li>
           55 </ul>
           56 <h2>What it does</h2>
           57 <ul>
           58 <li>Process a mail with the attachments automatically.</li>
           59 <li>The script processes the attachments in the mail and stores them.</li>
           60 <li>It will mail (back) the URL where the file(s) are stored.</li>
           61 </ul>
           62 <p>This script is tested on OpenBSD using OpenBSD smtpd and OpenBSD httpd and the
           63 gopher daemon geomyidae.</p>
           64 <h2>Install dependencies</h2>
           65 <p>On OpenBSD:</p>
           66 <pre><code>pkg_add mblaze
           67 </code></pre>
           68 <h2>smtpd mail configuration</h2>
           69 <p>In your mail aliases (for example /etc/mail/aliases) put:</p>
           70 <pre><code>paste: |/usr/local/bin/paste-mail
           71 </code></pre>
           72 <p>This pipes the mail to the script paste-mail for processing, this script is
           73 described below. Copy the below contents in /usr/local/bin/paste-mail</p>
           74 <p>Script:</p>
           75 <pre><code>#!/bin/sh
           76 
           77 d="/home/www/domains/www.codemadness.org/htdocs/mailpaste"
           78 tmpmsg=$(mktemp)
           79 tmpmail=$(mktemp)
           80 
           81 cleanup() {
           82         rm -f "$tmpmail" "$tmpmsg"
           83 }
           84 
           85 # store whole mail from stdin temporarily, on exit remove temporary file.
           86 trap "cleanup" EXIT
           87 cat &gt; "$tmpmail"
           88 
           89 # mblaze: don't store mail sequence.
           90 MAILSEQ=/dev/null
           91 export MAILSEQ
           92 
           93 # get from address (without display name).
           94 from=$(maddr -a -h 'From' /dev/stdin &lt; "$tmpmail")
           95 
           96 # check if allowed or not.
           97 case "$from" in
           98 "hiltjo@codemadness.org")
           99         ;;
          100 *)
          101         exit 0;;
          102 esac
          103 
          104 # prevent mail loop.
          105 if printf '%s' "$from" | grep -q "paste@"; then
          106         exit 0
          107 fi
          108 
          109 echo "Thank you for using the enterprise paste service." &gt; "$tmpmsg"
          110 echo "" &gt;&gt; "$tmpmsg"
          111 echo "Your file(s) are available at:" &gt;&gt; "$tmpmsg"
          112 echo "" &gt;&gt; "$tmpmsg"
          113 
          114 # process each attachment.
          115 mshow -n -q -t /dev/stdin &lt; "$tmpmail" | sed -nE 's@.*name="(.*)".*@\1@p' | while read -r name; do
          116         test "$name" = "" &amp;&amp; continue
          117 
          118         # extract attachment.
          119         tmpfile=$(mktemp -p "$d" XXXXXXXXXXXX)
          120         mshow -n -O /dev/stdin "$name" &lt; "$tmpmail" &gt; "$tmpfile"
          121 
          122         # use file extension.
          123         ext="${name##*/}"
          124         case "$ext" in
          125         *.tar.*)
          126                 # special case: support .tar.gz, tar.bz2, etc.
          127                 ext="tar.${ext##*.}";;
          128         *.*)
          129                 ext="${ext##*.}";;
          130         *)
          131                 ext="";;
          132         esac
          133         ext="${ext%%*.}"
          134 
          135         # use file extension if it is set.
          136         outputfile="$tmpfile"
          137         if test "$ext" != ""; then
          138                 outputfile="$tmpfile.$ext"
          139         fi
          140         mv "$tmpfile" "$outputfile"
          141         b=$(basename "$outputfile")
          142 
          143         chmod 666 "$outputfile"
          144         url="gopher://codemadness.org/9/mailpaste/$b"
          145 
          146         echo "$name:" &gt;&gt; "$tmpmsg"
          147         echo "        Text   file: gopher://codemadness.org/0/mailpaste/$b" &gt;&gt; "$tmpmsg"
          148         echo "        Image  file: gopher://codemadness.org/I/mailpaste/$b" &gt;&gt; "$tmpmsg"
          149         echo "        Binary file: gopher://codemadness.org/9/mailpaste/$b" &gt;&gt; "$tmpmsg"
          150         echo "" &gt;&gt; "$tmpmsg"
          151 done
          152 
          153 echo "" &gt;&gt; "$tmpmsg"
          154 echo "Sincerely," &gt;&gt; "$tmpmsg"
          155 echo "Your friendly paste_bot" &gt;&gt; "$tmpmsg"
          156 
          157 # mail back the user.
          158 mail -r "$from" -s "Your files" "$from" &lt; "$tmpmsg"
          159 
          160 cleanup
          161 </code></pre>
          162 <p>The mail daemon processing the mail needs of course to be able to have
          163 permissions to write to the specified directory. The user who received the mail
          164 needs to be able to read it from a location they can access and have
          165 permissions for it also.</p>
          166 <h2>Room for improvements</h2>
          167 <p>This is just an example script. There is room for many improvements.
          168 Feel free to change it in any way you like.</p>
          169 <h2>References</h2>
          170 <ul>
          171 <li><a href="https://man.openbsd.org/aliases">https://man.openbsd.org/aliases</a></li>
          172 <li><a href="https://man.openbsd.org/smtpd">https://man.openbsd.org/smtpd</a></li>
          173 <li><a href="https://man.openbsd.org/httpd">https://man.openbsd.org/httpd</a></li>
          174 <li><a href="https://github.com/leahneukirchen/mblaze">https://github.com/leahneukirchen/mblaze</a></li>
          175 </ul>
          176 <h2>Bye bye</h2>
          177 <p>I hope this enterprise(tm) mail service is inspirational or something ;)</p>
          178 
          179                         </article>
          180                 </div>
          181         </main>
          182 </body>
          183 </html>