d79161153086ea8e75cd3e7596c0006cff414009
[nit.git] / examples / curl_mail.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2013 Matthieu Lucas <lucasmatthieu@gmail.com>
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 # Mail sender sample using the Curl module
18 module curl_mail
19
20 import curl
21
22 var curl = new Curl
23 var mail_request = new CurlMailRequest(curl)
24
25 # Networks
26 var response: nullable CurlResponse = mail_request.set_outgoing_server("smtps://smtp.example.org:465", "user@example.org", "mypassword")
27 if response isa CurlResponseFailed then
28 print "Error code : {response.error_code}"
29 print "Error msg : {response.error_msg}"
30 end
31
32 # Headers
33 mail_request.from = "Billy Bob"
34 mail_request.to = ["user@example.org"]
35 mail_request.cc = ["bob@example.org"]
36 mail_request.bcc = null
37
38 var headers_body = new HeaderMap
39 headers_body["Content-Type:"] = "text/html; charset=\"UTF-8\""
40 headers_body["Content-Transfer-Encoding:"] = "quoted-printable"
41 mail_request.headers_body = headers_body
42
43 # Content
44 mail_request.body = "<h1>Here you can write HTML stuff.</h1>"
45 mail_request.subject = "Hello From My Nit Program"
46
47 # Others
48 mail_request.verbose = false
49
50 # Send mail
51 response = mail_request.execute
52 if response isa CurlResponseFailed then
53 print "Error code : {response.error_code}"
54 print "Error msg : {response.error_msg}"
55 else if response isa CurlMailResponseSuccess then
56 print "Mail Sent"
57 else
58 print "Unknown Curl Response type"
59 end