Property definitions

sendmail $ Mail :: defaultinit
# An email to be sent using `sendmail`
class Mail

	# Sender of this email
	var from: String is writable

	# Recipients of the message
	var to = new Array[String]

	# Recipients of a carbon copy of the message
	var cc = new Array[String]

	# Recipients of a blind carbon copy of the message
	var bcc = new Array[String]

	# Subject of this mail
	var subject: String is writable

	# Content of this mail
	var content: String is writable

	# Metadata in the header of this mail
	var header = new HashMap[String, String]

	# Should the content of this email be encrypted using base64?
	#
	# This will also set the header `Content-Transfer-Encoding` to base64.
	# By default, the encoding is 8bit.
	var encrypt_with_base64 = false

	# Send this mail using the `sendmail` command
	#
	# require: `sendmail_is_available`
	fun send: Bool
	do
		assert sendmail_is_available

		# All recipients
		var all = new Array[String]
		all.add_all to
		all.add_all cc
		all.add_all bcc

		var proc = new ProcessWriter("sendmail", all.join(","))
		if proc.is_writable then proc.write to_s
		proc.close
		proc.wait
		var status = proc.status
		return status == 0
	end

	redef fun to_s
	do
		# Set encoding (and encode if needed)
		var content = content
		var encoding
		if encrypt_with_base64 then
			content = content.encode_base64
			encoding = "base64"
		else
			encoding = "8bit"
		end
		header["Content-Transfer-Encoding"] = encoding

		# Generate expected format
		return """
From: {{{from}}}\r
To: {{{to.join(",")}}}\r
CC: {{{cc.join(",")}}}\r
BCC: {{{bcc.join(",")}}}\r
Subject: {{{subject}}}\r
{{{header.join("\r\n", ": ")}}}\r\n\r
{{{content}}}"""
	end
end
lib/sendmail/sendmail.nit:34,1--108,3