<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<atom:link href="http://dev1galaxy.org/extern.php?action=feed&amp;tid=1892&amp;type=rss" rel="self" type="application/rss+xml" />
		<title><![CDATA[Dev1 Galaxy Forum / simple way to send email from terminal or script]]></title>
		<link>http://dev1galaxy.org/viewtopic.php?id=1892</link>
		<description><![CDATA[The most recent posts in simple way to send email from terminal or script.]]></description>
		<lastBuildDate>Mon, 19 Feb 2018 15:50:42 +0000</lastBuildDate>
		<generator>FluxBB</generator>
		<item>
			<title><![CDATA[simple way to send email from terminal or script]]></title>
			<link>http://dev1galaxy.org/viewtopic.php?pid=7662#p7662</link>
			<description><![CDATA[<p>Devuan has inspired me to do all my computing as simply as possible. Here&#039;s a little script I wrote so that I can send email from a script or terminal with virtually zero overhead (specifically, without having to install and configure an MTA such as exim or postfix). All you need is:</p><p>1. sudo apt-get install curl<br />2. put your smtp credentials at the top of the script<br />3. make this script executable and put it in your PATH (on my system, the script is called easymail)</p><p>I thought I&#039;d share it here in case anyone else has use for it. If you see anything in it that could be improved, please let me know.</p><p>Cheers, <br />Bruno</p><div class="codebox"><pre class="vscroll"><code>#!/bin/sh

# Purpose: To send email as simply as possible (i.e., without needing to install an MTA 
# such as exim or postfix). The script&#039;s only dependencies are curl and coreutils on Linux,
# curl and gcoreutils on OpenBSD.
#
# Note about regular vs. root user: Either one can use this script :)
#
# Note about CA ssl certificates on computer/phone:
# If they are missing, add --insecure to the curl command.
#
# Author: Bruno &quot;GNUser&quot; Dantas
# License: GPLv3
# Version: 3.0 (27Mar2019)
# Changelog:
#	- working on Devuan ASCII and OpenBSD 6.4
#	- body not mandatory
#	- no bashisms

# smtp credentials
host=&#039;smtps://my.email.com&#039;
port=&#039;993&#039;
user=&#039;my-username&#039;
password=&#039;secretPasswoRd&#039;

main()
{
	get_stdin
	get_commandline_options &quot;$@&quot;
	build_eml_file
	wait_for_internet
	send_eml_file
}

show_usage()
{
	echo &quot;Usage: easymail -d &lt;destination&gt; [-s &lt;subject&gt;] [-b &lt;body&gt;] [-a &lt;attachment&gt;]

Notes:
 - destination: an email address (if multiple, precede each one with -d)
 - subject: a string
 - body: stdin, -b string, or -b file (note: -b flag takes precedence over stdin). examples:
	 1. echo &#039;some string&#039; | easymail
	 2. easymail &lt;/path/to/some-text-file
	 3. easymail -b &#039;some string&#039;
	 4. easymail -b /path/to/some-text-file
 - attachment: a file (if multiple, precede each one with -a)

Example:
 easymail -d somebody@somewhere.com -s &#039;Devuan&#039; -b &#039;Devuan rocks!&#039; -a fork.pdf&quot;
}

attach()
{
	# create an attachments file if one does not exist yet
	[ -z $attachments_file ] &amp;&amp; attachments_file=$(mktemp)

	# now append the attachment to the attachments file
	filename=&quot;$(basename &quot;$1&quot;)&quot;
	echo &quot;------7II5XTH4IPXG2QZOY8LJF98QQX4IR3
Content-Transfer-Encoding: base64
Content-Type: application/octet-stream;
 name=\&quot;$filename\&quot;
Content-Disposition: attachment;
 filename=\&quot;$filename\&quot;;
&quot; &gt;&gt;$attachments_file

	if uname | grep -qi BSD; then
		cat &quot;$1&quot; | b64encode poop | tail -n +2 &gt;&gt;$attachments_file
	else # Linux
		cat &quot;$1&quot; | base64 &gt;&gt;$attachments_file
	fi
}

cleanup()
{
	rm -f $eml_file $attachments_file
}
trap cleanup EXIT HUP TERM INT

get_stdin()
{
	if uname | grep -qi BSD; then
		body=&quot;$(gtimeout 1 dd bs=1 count=1 2&gt;/dev/null &amp;&amp; cat)&quot;
	else # Linux
		body=&quot;$(timeout 1 dd bs=1 count=1 2&gt;/dev/null &amp;&amp; cat)&quot;
	fi

	# dd has 1 sec to get 1 byte, which is plenty of time if there is data
	# in stdin waiting to be read. If dd succeeds, it passes its byte to stdout
	# and triggers cat, which has unlimited time to get remaining data.
	# This seems complicated but is necessary: using just cat causes script to 
	# hang if there is no data in stdin.
}

get_commandline_options()
{
	while [ &quot;$#&quot; -gt 0 ]; do
		case &quot;$1&quot; in
			-d)	
				curl_destination=&quot;${curl_destination}--mail-rcpt $2 &quot;
				header_destination=&quot;${header_destination}$2, &quot;
				shift 2
				;;
			-s) 
				subject=&quot;$2&quot;
				shift 2
				;;
			-b) 
				body=&quot;$2&quot;
				shift 2
				;;
			-a) 
				attach &quot;$2&quot;
				shift 2
				;;
			-*)
				echo &quot;Invalid option: $1&quot;
				shift
				;;
			*)
				shift
				;;
		esac
	done
}

build_eml_file()
{
	# create header
	eml_file=$(mktemp)
	echo &quot;MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=\&quot;----7II5XTH4IPXG2QZOY8LJF98QQX4IR3\&quot;
Content-Transfer-Encoding: 8bit
Subject: $subject
From: Easymail &lt;easymail@suckless.nix&gt;
Date: $(date)
To: $header_destination

------7II5XTH4IPXG2QZOY8LJF98QQX4IR3
Content-Transfer-Encoding: 8bit
Content-Type: text/plain;
 charset=UTF-8
&quot; &gt;$eml_file

	# append body
	if [ -f &quot;$body&quot; ]; then
		cat &quot;$body&quot; &gt;&gt;$eml_file
	else
		echo &quot;$body&quot; &gt;&gt;$eml_file
	fi

	# append attachment(s) if applicable
	if [ -f &quot;$attachments_file&quot; ]; then
		cat &quot;$attachments_file&quot; &gt;&gt;$eml_file
	fi
}

wait_for_internet()
{
	while true; do
		wget --spider --timeout=2 -o /dev/null duckduckgo.com &amp;&amp; return 0
		sleep 1
	done
}

send_eml_file()
{
	curl --url ${host}:${port} --ssl-reqd --upload-file $eml_file $curl_destination --user ${user}:${password} &gt;/dev/null 2&gt;&amp;1
}

if [ &quot;$#&quot; -le 1 ]; then
	show_usage
else
	main &quot;$@&quot;
fi</code></pre></div>]]></description>
			<author><![CDATA[dummy@example.com (GNUser)]]></author>
			<pubDate>Mon, 19 Feb 2018 15:50:42 +0000</pubDate>
			<guid>http://dev1galaxy.org/viewtopic.php?pid=7662#p7662</guid>
		</item>
	</channel>
</rss>
