add mail service hack/article - 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 --- (DIR) commit 1ba78ca4da4fbf1f531e892c9d90289ea10d49eb (DIR) parent 93ea4a150da7b2d028f294ab642434d2158d8884 (HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org> Date: Wed, 25 Oct 2023 12:27:42 +0200 add mail service hack/article Diffstat: M output/atom.xml | 12 ++++++++++++ M output/atom_content.xml | 132 +++++++++++++++++++++++++++++++ M output/index | 1 + M output/index.html | 1 + M output/rss.xml | 8 ++++++++ M output/rss_content.xml | 127 +++++++++++++++++++++++++++++++ M output/sitemap.xml | 4 ++++ M output/twtxt.txt | 1 + M output/urllist.txt | 1 + A pages/mailservice.cfg | 6 ++++++ A pages/mailservice.md | 136 +++++++++++++++++++++++++++++++ 11 files changed, 429 insertions(+), 0 deletions(-) --- (DIR) diff --git a/output/atom.xml b/output/atom.xml @@ -7,6 +7,18 @@ <id>https://www.codemadness.org/atom.xml</id> <link rel="self" type="application/atom+xml" href="https://www.codemadness.org/atom.xml" /> <entry> + <title>Setup your own mail paste service</title> + <link rel="alternate" type="text/html" href="https://www.codemadness.org/mailservice.html" /> + <id>https://www.codemadness.org/mailservice.html</id> + <updated>2023-10-25T00:00:00Z</updated> + <published>2023-10-25T00:00:00Z</published> + <author> + <name>Hiltjo</name> + <uri>https://www.codemadness.org</uri> + </author> + <summary>Setup your own mail paste service using mblaze</summary> +</entry> +<entry> <title>A simple TODO application</title> <link rel="alternate" type="text/html" href="https://www.codemadness.org/todo-application.html" /> <id>https://www.codemadness.org/todo-application.html</id> (DIR) diff --git a/output/atom_content.xml b/output/atom_content.xml @@ -7,6 +7,138 @@ <id>https://www.codemadness.org/atom_content.xml</id> <link rel="self" type="application/atom+xml" href="https://www.codemadness.org/atom_content.xml" /> <entry> + <title>Setup your own mail paste service</title> + <link rel="alternate" type="text/html" href="https://www.codemadness.org/mailservice.html" /> + <id>https://www.codemadness.org/mailservice.html</id> + <updated>2023-10-25T00:00:00Z</updated> + <published>2023-10-25T00:00:00Z</published> + <author> + <name>Hiltjo</name> + <uri>https://www.codemadness.org</uri> + </author> + <summary>Setup your own mail paste service using mblaze</summary> + <content type="html"><![CDATA[<h1>Setup your own mail paste service</h1> + <p><strong>Last modification on </strong> <time>2023-10-25</time></p> + <h2>How it works</h2> +<ul> +<li>Send a mail with an attachment to a certain mail address.</li> +<li>The mail daemon configuration has an mail alias to pipe the raw mail to a +shellscript.</li> +<li>This shellscript processes the raw mail contents from stdin.</li> +</ul> +<h2>What it does</h2> +<ul> +<li>Process a mail with an attachment automatically. In this example only one +attachment is supported.</li> +<li>The script processes the attachment in the mail and stores it.</li> +<li>It will mail (back) the URL where the file is stored, for now to one static +mail address.</li> +</ul> +<p>This script is tested on OpenBSD using OpenBSD smtpd and OpenBSD httpd and the +gopher daemon geomyidae.</p> +<h2>Install dependencies</h2> +<p>On OpenBSD:</p> +<pre><code>pkg_add mblaze +</code></pre> +<h2>smtpd mail configuration</h2> +<p>In your mail aliases (for example /etc/mail/aliases) put:</p> +<pre><code>paste: |/usr/local/bin/paste-mail +</code></pre> +<p>This pipes the mail to the script paste-mail for processing, this script is +described below.</p> +<p>Script:</p> +<pre><code>#!/bin/sh + +d="/home/www/domains/www.codemadness.org/htdocs/mailpaste" +tmpfile=$(mktemp -p "$d" XXXXXXXXXXXX) +tmpmail=$(mktemp) + +# store mail temporarily, on exit remove temporary file. +trap "rm -f $tmpmail" EXIT +cat > "$tmpmail" + +# don't store mail sequence. +MAILSEQ=/dev/null +export MAILSEQ + +# get from address. +from=$(maddr -h 'From' /dev/stdin < "$tmpmail") + +# prevent mail loop. +if printf '%s' "$from" | grep -q "paste@"; then + exit 0 +fi + +# extract (first) attachment. +mshow -n -O /dev/stdin "*" < "$tmpmail" > "$tmpfile" + +# determine extension by mimetype. +mimetype=$(file -b -i "$tmpfile") +case "$mimetype" in \ +"application/zip") + ext="zip";; +"application/pdf") + ext="pdf";; +"*/png") + ext="png";; +"*/gif") + ext="gif";; +"text/*") + ext="txt";; +*) + ext="bin";; +esac + +file="$tmpfile.$ext" +mv "$tmpfile" "$file" +b=$(basename "$file") + +chmod 666 "$file" + +# mail back the user. + +url="gopher://codemadness.org/9/mailpaste/$b" + +mail -r hiltjo@codemadness.org -s "Your file: $url" "$from" <<!__EOF__ +Thank you for using the enterprise paste service. + +Your file is available at: + +Text file: gopher://codemadness.org/0/mailpaste/$b +Image file: gopher://codemadness.org/I/mailpaste/$b +Binary file: gopher://codemadness.org/9/mailpaste/$b + +Sincerely, +Your friendly paste_bot +!__EOF__ + +rm -f "$tmpmail" +</code></pre> +<p>The mail daemon processing the mail needs of course 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.</p> +<h2>Room for improvements</h2> +<p>Some ideas for improvements:</p> +<ul> +<li>Handle multiple attachments.</li> +<li>Handle mailing back to more users, while using a safe (whitelist?) mechanism +to prevent spam bots etc.</li> +<li>Handling of more filetypes and file extension in a smarter way.</li> +<li>Modify it to any preferences you might have.</li> +</ul> +<h2>References</h2> +<ul> +<li><a href="https://man.openbsd.org/aliases">https://man.openbsd.org/aliases</a></li> +<li><a href="https://man.openbsd.org/smtpd">https://man.openbsd.org/smtpd</a></li> +<li><a href="https://man.openbsd.org/httpd">https://man.openbsd.org/httpd</a></li> +<li><a href="https://github.com/leahneukirchen/mblaze">https://github.com/leahneukirchen/mblaze</a></li> +</ul> +<h2>Bye bye</h2> +<p>I hope this enterprise(tm) mail service is inspirational or something ;)</p> +]]></content> +</entry> +<entry> <title>A simple TODO application</title> <link rel="alternate" type="text/html" href="https://www.codemadness.org/todo-application.html" /> <id>https://www.codemadness.org/todo-application.html</id> (DIR) diff --git a/output/index b/output/index @@ -11,6 +11,7 @@ i codemadness.org 70 i codemadness.org 70 iPhlog posts codemadness.org 70 i codemadness.org 70 +12023-10-25 Setup your own mail paste service /phlog/mailservice codemadness.org 70 12022-07-01 A simple TODO application /phlog/todo codemadness.org 70 12022-03-23 2FA TOTP without crappy authenticator apps /phlog/totp codemadness.org 70 12021-10-23 Setup an OpenBSD RISCV64 VM in QEMU /phlog/openbsd-riscv64-vm codemadness.org 70 (DIR) diff --git a/output/index.html b/output/index.html @@ -40,6 +40,7 @@ <div id="main"> <h1>Posts</h1> <table> +<tr><td><time>2023-10-25</time></td><td><a href="mailservice.html">Setup your own mail paste service</a></td></tr> <tr><td><time>2022-07-01</time></td><td><a href="todo-application.html">A simple TODO application</a></td></tr> <tr><td><time>2022-03-23</time></td><td><a href="totp.html">2FA TOTP without crappy authenticator apps</a></td></tr> <tr><td><time>2021-10-23</time></td><td><a href="openbsd-riscv64-vm.html">Setup an OpenBSD RISCV64 VM in QEMU</a></td></tr> (DIR) diff --git a/output/rss.xml b/output/rss.xml @@ -7,6 +7,14 @@ <description>blog with various projects and articles about computer-related things</description> <link>https://www.codemadness.org</link> <item> + <title>Setup your own mail paste service</title> + <link>https://www.codemadness.org/mailservice.html</link> + <guid>https://www.codemadness.org/mailservice.html</guid> + <dc:date>2023-10-25T00:00:00Z</dc:date> + <author>Hiltjo</author> + <description>Setup your own mail paste service using mblaze</description> +</item> +<item> <title>A simple TODO application</title> <link>https://www.codemadness.org/todo-application.html</link> <guid>https://www.codemadness.org/todo-application.html</guid> (DIR) diff --git a/output/rss_content.xml b/output/rss_content.xml @@ -7,6 +7,133 @@ <description>blog with various projects and articles about computer-related things</description> <link>https://www.codemadness.org</link> <item> + <title>Setup your own mail paste service</title> + <link>https://www.codemadness.org/mailservice.html</link> + <guid>https://www.codemadness.org/mailservice.html</guid> + <dc:date>2023-10-25T00:00:00Z</dc:date> + <author>Hiltjo</author> + <description><![CDATA[<h1>Setup your own mail paste service</h1> + <p><strong>Last modification on </strong> <time>2023-10-25</time></p> + <h2>How it works</h2> +<ul> +<li>Send a mail with an attachment to a certain mail address.</li> +<li>The mail daemon configuration has an mail alias to pipe the raw mail to a +shellscript.</li> +<li>This shellscript processes the raw mail contents from stdin.</li> +</ul> +<h2>What it does</h2> +<ul> +<li>Process a mail with an attachment automatically. In this example only one +attachment is supported.</li> +<li>The script processes the attachment in the mail and stores it.</li> +<li>It will mail (back) the URL where the file is stored, for now to one static +mail address.</li> +</ul> +<p>This script is tested on OpenBSD using OpenBSD smtpd and OpenBSD httpd and the +gopher daemon geomyidae.</p> +<h2>Install dependencies</h2> +<p>On OpenBSD:</p> +<pre><code>pkg_add mblaze +</code></pre> +<h2>smtpd mail configuration</h2> +<p>In your mail aliases (for example /etc/mail/aliases) put:</p> +<pre><code>paste: |/usr/local/bin/paste-mail +</code></pre> +<p>This pipes the mail to the script paste-mail for processing, this script is +described below.</p> +<p>Script:</p> +<pre><code>#!/bin/sh + +d="/home/www/domains/www.codemadness.org/htdocs/mailpaste" +tmpfile=$(mktemp -p "$d" XXXXXXXXXXXX) +tmpmail=$(mktemp) + +# store mail temporarily, on exit remove temporary file. +trap "rm -f $tmpmail" EXIT +cat > "$tmpmail" + +# don't store mail sequence. +MAILSEQ=/dev/null +export MAILSEQ + +# get from address. +from=$(maddr -h 'From' /dev/stdin < "$tmpmail") + +# prevent mail loop. +if printf '%s' "$from" | grep -q "paste@"; then + exit 0 +fi + +# extract (first) attachment. +mshow -n -O /dev/stdin "*" < "$tmpmail" > "$tmpfile" + +# determine extension by mimetype. +mimetype=$(file -b -i "$tmpfile") +case "$mimetype" in \ +"application/zip") + ext="zip";; +"application/pdf") + ext="pdf";; +"*/png") + ext="png";; +"*/gif") + ext="gif";; +"text/*") + ext="txt";; +*) + ext="bin";; +esac + +file="$tmpfile.$ext" +mv "$tmpfile" "$file" +b=$(basename "$file") + +chmod 666 "$file" + +# mail back the user. + +url="gopher://codemadness.org/9/mailpaste/$b" + +mail -r hiltjo@codemadness.org -s "Your file: $url" "$from" <<!__EOF__ +Thank you for using the enterprise paste service. + +Your file is available at: + +Text file: gopher://codemadness.org/0/mailpaste/$b +Image file: gopher://codemadness.org/I/mailpaste/$b +Binary file: gopher://codemadness.org/9/mailpaste/$b + +Sincerely, +Your friendly paste_bot +!__EOF__ + +rm -f "$tmpmail" +</code></pre> +<p>The mail daemon processing the mail needs of course 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.</p> +<h2>Room for improvements</h2> +<p>Some ideas for improvements:</p> +<ul> +<li>Handle multiple attachments.</li> +<li>Handle mailing back to more users, while using a safe (whitelist?) mechanism +to prevent spam bots etc.</li> +<li>Handling of more filetypes and file extension in a smarter way.</li> +<li>Modify it to any preferences you might have.</li> +</ul> +<h2>References</h2> +<ul> +<li><a href="https://man.openbsd.org/aliases">https://man.openbsd.org/aliases</a></li> +<li><a href="https://man.openbsd.org/smtpd">https://man.openbsd.org/smtpd</a></li> +<li><a href="https://man.openbsd.org/httpd">https://man.openbsd.org/httpd</a></li> +<li><a href="https://github.com/leahneukirchen/mblaze">https://github.com/leahneukirchen/mblaze</a></li> +</ul> +<h2>Bye bye</h2> +<p>I hope this enterprise(tm) mail service is inspirational or something ;)</p> +]]></description> +</item> +<item> <title>A simple TODO application</title> <link>https://www.codemadness.org/todo-application.html</link> <guid>https://www.codemadness.org/todo-application.html</guid> (DIR) diff --git a/output/sitemap.xml b/output/sitemap.xml @@ -1,6 +1,10 @@ <?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> + <loc>https://www.codemadness.org/mailservice.html</loc> + <lastmod>2023-10-25</lastmod> +</url> +<url> <loc>https://www.codemadness.org/todo-application.html</loc> <lastmod>2022-07-01</lastmod> </url> (DIR) diff --git a/output/twtxt.txt b/output/twtxt.txt @@ -1,3 +1,4 @@ +2023-10-25T00:00:00Z Setup your own mail paste service: https://www.codemadness.org/mailservice.html 2022-07-01T00:00:00Z A simple TODO application: https://www.codemadness.org/todo-application.html 2022-03-23T00:00:00Z 2FA TOTP without crappy authenticator apps: https://www.codemadness.org/totp.html 2021-10-23T00:00:00Z Setup an OpenBSD RISCV64 VM in QEMU: https://www.codemadness.org/openbsd-riscv64-vm.html (DIR) diff --git a/output/urllist.txt b/output/urllist.txt @@ -1,3 +1,4 @@ +https://www.codemadness.org/mailservice.html https://www.codemadness.org/todo-application.html https://www.codemadness.org/totp.html https://www.codemadness.org/openbsd-riscv64-vm.html (DIR) diff --git a/pages/mailservice.cfg b/pages/mailservice.cfg @@ -0,0 +1,6 @@ +title = Setup your own mail paste service +id = mailservice +description = Setup your own mail paste service using mblaze +keywords = mail, paste, service +created = 2023-10-25 +updated = 2023-10-25 (DIR) diff --git a/pages/mailservice.md b/pages/mailservice.md @@ -0,0 +1,136 @@ +## How it works + +* Send a mail with an attachment to a certain mail address. +* 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 an attachment automatically. In this example only one + attachment is supported. +* The script processes the attachment in the mail and stores it. +* It will mail (back) the URL where the file is stored, for now to one static + mail address. + +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. + + +Script: + + #!/bin/sh + + d="/home/www/domains/www.codemadness.org/htdocs/mailpaste" + tmpfile=$(mktemp -p "$d" XXXXXXXXXXXX) + tmpmail=$(mktemp) + + # store mail temporarily, on exit remove temporary file. + trap "rm -f $tmpmail" EXIT + cat > "$tmpmail" + + # don't store mail sequence. + MAILSEQ=/dev/null + export MAILSEQ + + # get from address. + from=$(maddr -h 'From' /dev/stdin < "$tmpmail") + + # prevent mail loop. + if printf '%s' "$from" | grep -q "paste@"; then + exit 0 + fi + + # extract (first) attachment. + mshow -n -O /dev/stdin "*" < "$tmpmail" > "$tmpfile" + + # determine extension by mimetype. + mimetype=$(file -b -i "$tmpfile") + case "$mimetype" in \ + "application/zip") + ext="zip";; + "application/pdf") + ext="pdf";; + "*/png") + ext="png";; + "*/gif") + ext="gif";; + "text/*") + ext="txt";; + *) + ext="bin";; + esac + + file="$tmpfile.$ext" + mv "$tmpfile" "$file" + b=$(basename "$file") + + chmod 666 "$file" + + # mail back the user. + + url="gopher://codemadness.org/9/mailpaste/$b" + + mail -r hiltjo@codemadness.org -s "Your file: $url" "$from" <<!__EOF__ + Thank you for using the enterprise paste service. + + Your file is available at: + + Text file: gopher://codemadness.org/0/mailpaste/$b + Image file: gopher://codemadness.org/I/mailpaste/$b + Binary file: gopher://codemadness.org/9/mailpaste/$b + + Sincerely, + Your friendly paste_bot + !__EOF__ + + rm -f "$tmpmail" + + +The mail daemon processing the mail needs of course 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 + +Some ideas for improvements: + +* Handle multiple attachments. +* Handle mailing back to more users, while using a safe (whitelist?) mechanism + to prevent spam bots etc. +* Handling of more filetypes and file extension in a smarter way. +* Modify it to any preferences you might have. + + +## 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 ;)