1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Send emails using the `sendmail` program
21 # if sendmail_is_available then
22 # var mail = new Mail("from@example.com", "Title", "Content")
23 # mail.to.add "to@example.org"
25 # else print "please install sendmail"
31 # Can we find the external program `sendmail`?
32 fun sendmail_is_available
: Bool do return "sendmail".program_is_in_path
34 # An email to be sent using `sendmail`
37 # Sender of this email
38 var from
: String is writable
40 # Recipients of the message
41 var to
= new Array[String]
43 # Recipients of a carbon copy of the message
44 var cc
= new Array[String]
46 # Recipients of a blind carbon copy of the message
47 var bcc
= new Array[String]
49 # Subject of this mail
50 var subject
: String is writable
52 # Content of this mail
53 var content
: String is writable
55 # Metadata in the header of this mail
56 var header
= new HashMap[String, String]
58 # Should the content of this email be encrypted using base64?
60 # This will also set the header `Content-Transfer-Encoding` to base64.
61 # By default, the encoding is 8bit.
62 var encrypt_with_base64
= false
64 # Send this mail using the `sendmail` command
66 # require: `sendmail_is_available`
69 assert sendmail_is_available
72 var all
= new Array[String]
77 var proc
= new ProcessWriter("sendmail", all
.join
(","))
78 if proc
.is_writable
then proc
.write to_s
81 var status
= proc
.status
87 # Set encoding (and encode if needed)
90 if encrypt_with_base64
then
91 content
= content
.encode_base64
96 header
["Content-Transfer-Encoding"] = encoding
98 # Generate expected format
101 To: {{{to.join(",")}}}\r
102 CC: {{{cc.join(",")}}}\r
103 BCC: {{{bcc.join(",")}}}\r
104 Subject: {{{subject}}}\r
105 {{{header.join("\r\n", ": ")}}}\r\n\r