-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathsendmail.nit
108 lines (91 loc) · 2.67 KB
/
sendmail.nit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# This file is part of NIT ( http://www.nitlanguage.org ).
#
# Copyright 2014 Alexis Laferrière <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Send emails using the `sendmail` program
#
# Usage example:
# ~~~~
# if sendmail_is_available then
# var mail = new Mail("[email protected]", "Title", "Content")
# mail.to.add "[email protected]"
# mail.send
# else print "please install sendmail"
# ~~~~
module sendmail
import base64
# Can we find the external program `sendmail`?
fun sendmail_is_available: Bool do return "sendmail".program_is_in_path
# 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