misc/vim: inform the user when no results are found
[nit.git] / lib / sendmail.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 # Send emails using the `sendmail` program
18 #
19 # Usage example:
20 # ~~~~
21 # if sendmail_is_available then
22 # var mail = new Mail("from@example.com", "Title", "Content")
23 # mail.to.add "to@example.org"
24 # mail.send
25 # else print "please install sendmail"
26 # ~~~~
27 module sendmail
28
29 import base64
30
31 # Can we find the external program `sendmail`?
32 fun sendmail_is_available: Bool do return "sendmail".program_is_in_path
33
34 # An email to be sent using `sendmail`
35 class Mail
36
37 # Sender of this email
38 var from: String is writable
39
40 # Recipients of the message
41 var to = new Array[String]
42
43 # Recipients of a carbon copy of the message
44 var cc = new Array[String]
45
46 # Recipients of a blind carbon copy of the message
47 var bcc = new Array[String]
48
49 # Subject of this mail
50 var subject: String is writable
51
52 # Content of this mail
53 var content: String is writable
54
55 # Metadata in the header of this mail
56 var header = new HashMap[String, String]
57
58 # Should the content of this email be encrypted using base64?
59 #
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
63
64 # Send this mail using the `sendmail` command
65 #
66 # require: `sendmail_is_available`
67 fun send: Bool
68 do
69 assert sendmail_is_available
70
71 # All recipients
72 var all = new Array[String]
73 all.add_all to
74 all.add_all cc
75 all.add_all bcc
76
77 var proc = new ProcessWriter("sendmail", all.join(","))
78 if proc.is_writable then proc.write to_s
79 proc.close
80 proc.wait
81 var status = proc.status
82 return status == 0
83 end
84
85 redef fun to_s
86 do
87 # Set encoding (and encode if needed)
88 var content = content
89 var encoding
90 if encrypt_with_base64 then
91 content = content.encode_base64
92 encoding = "base64"
93 else
94 encoding = "8bit"
95 end
96 header["Content-Transfer-Encoding"] = encoding
97
98 # Generate expected format
99 return """
100 From: {{{from}}}\r
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
106 {{{content}}}"""
107 end
108 end