The officially official Devuan Forum!

You are not logged in.

#1 2018-02-19 15:50:42

GNUser
Member
Registered: 2017-03-16
Posts: 333  

script to send email without MTA

Devuan has inspired me to do all my computing as simply as possible. Here's a little script I wrote so that I can send emails from scripts/terminal with virtually zero overhead (specifically, no need for exim, postfix, mutt, or the like). All you need is:

1. sudo apt-get install curl
2. put your smtp credentials at the top of the script
3. make this script executable and put it in your PATH (on my system, the script is called easymail)

I thought I'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.

Cheers,
Bruno

#!/bin/sh

# Purpose: To send email as simply as possible (i.e., without needing to install an MTA 
#+ such as exim or postfix). All that's required for this script to work is that curl
#+ be installed and your smtp credentials be given at the top of the script.

# Author: Bruno "GNUser" Dantas
# License: GPLv3
# Version: 1.5

# smtp credentials
host='smtps://smtp.someemail.com'
port='465'
user='username'
password='secreTpassworD'

main()
{
	check_stdin
	get_commandline_options "$@"
	build_eml_file
	send_eml_file
}

show_usage()
{
	echo "Usage: easymail -d <destination> [-s <subject>] [-b <body>] [-a <attachment>]

Notes:
 - destination: an email address (if multiple, precede each one with -d)
 - subject: a string
 - body: take your pick...
	 1. easymail -b 'some string'
	 2. easymail -b some-text-file
	 3. some-command | easymail
	 4. easymail <some-text-file
 - attachment: a file (if multiple, precede each one with -a)

Example:
 easymail -d somebody@somewhere.com -s 'Devuan' -b 'Devuan rocks!' -a fork.pdf"
}

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

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

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

check_stdin()
{
	# if stdin is connected to a pipe or a file, use that as body of email
	# i.e., if 'some-command | easymail' or 'easymail <some-file'

	stdin=$(readlink -f /proc/$$/fd/0)
	if ( (echo "$stdin" | grep -q pipe) || [ -f "$stdin" ] ); then
		body="$(cat)"
	fi
}

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

build_eml_file()
{
	# create header
	eml_file=$(mktemp)
	echo "MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=\"----7II5XTH4IPXG2QZOY8LJF98QQX4IR3\"
Content-Transfer-Encoding: 8bit
Subject: $subject
From: Easymail <easymail@gnu.linux>
Date: $(date)
To: $header_destination

------7II5XTH4IPXG2QZOY8LJF98QQX4IR3
Content-Transfer-Encoding: 8bit
Content-Type: text/plain;
 charset=UTF-8
" >$eml_file

	# append body
	if [ -f "$body" ]; then
		cat "$body" >>$eml_file
	else
		echo "$body" >>$eml_file
	fi

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

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

if [ "$#" -le 1 ]; then
	show_usage
else
	main "$@"
fi

Last edited by GNUser (2018-02-22 14:51:52)

Offline

Board footer