PageRenderTime 44ms CodeModel.GetById 12ms app.highlight 27ms RepoModel.GetById 1ms app.codeStats 0ms

/demo/src/view-email.adb

http://github.com/ThomasLocke/yolk
Ada | 158 lines | 110 code | 19 blank | 29 comment | 3 complexity | d40884ca6de0fc91bd50e02da7432922 MD5 | raw file
Possible License(s): AGPL-1.0
  1-------------------------------------------------------------------------------
  2--                                                                           --
  3--                   Copyright (C) 2010-, Thomas Løcke                   --
  4--                                                                           --
  5--  This is free software;  you can redistribute it and/or modify it         --
  6--  under terms of the  GNU General Public License  as published by the      --
  7--  Free Software  Foundation;  either version 3,  or (at your  option) any  --
  8--  later version. This library is distributed in the hope that it will be   --
  9--  useful, but WITHOUT ANY WARRANTY;  without even the implied warranty of  --
 10--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     --
 11--  You should have received a copy of the GNU General Public License and    --
 12--  a copy of the GCC Runtime Library Exception along with this program;     --
 13--  see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
 14--  <http://www.gnu.org/licenses/>.                                          --
 15--                                                                           --
 16-------------------------------------------------------------------------------
 17
 18with Ada.Strings.Fixed;
 19with AWS.Parameters;
 20with AWS.Templates;
 21with Yolk.Email.Composer;
 22with Yolk.Log;
 23
 24package body View.Email is
 25
 26   procedure Populate_Form
 27     (Address : in     String;
 28      Name    : in     String;
 29      T       :    out AWS.Templates.Translate_Set);
 30   --  Insert the form data associations into the T translate set.
 31
 32   procedure Send_Email
 33     (Address : in     String;
 34      Name    : in     String;
 35      T       :    out AWS.Templates.Translate_Set);
 36   --  Construct and send an email to recipient.
 37
 38   ---------------
 39   --  Generate --
 40   ---------------
 41
 42   function Generate
 43     (Request : in AWS.Status.Data)
 44      return AWS.Response.Data
 45   is
 46      use AWS.Templates;
 47      use Ada.Strings;
 48
 49      P       : constant AWS.Parameters.List :=
 50                  AWS.Status.Parameters (Request);
 51      Name    : constant String :=
 52                  Fixed.Trim (P.Get ("recip_name"), Both);
 53      Address : constant String :=
 54                  Fixed.Trim (P.Get ("recip_address"), Both);
 55      T       : Translate_Set;
 56   begin
 57      if Name'Length > 0
 58        and then Address'Length > 0
 59      then
 60         Send_Email (Address => Address,
 61                     Name    => Name,
 62                     T                 => T);
 63      else
 64         Populate_Form (Address => "yolk@mailinator.com",
 65                        Name    => "Zaphod Beeblebrox",
 66                        T                 => T);
 67      end if;
 68
 69      return Build_Response
 70        (Status_Data   => Request,
 71         Template_File => My.Config.Get (My.Template_Email),
 72         Translations  => T);
 73   end Generate;
 74
 75   ---------------------
 76   --  Populate_Form  --
 77   ---------------------
 78
 79   procedure Populate_Form
 80     (Address : in     String;
 81      Name    : in     String;
 82      T       :    out AWS.Templates.Translate_Set)
 83   is
 84      use AWS.Templates;
 85   begin
 86      Insert (T, Assoc ("RECIP_NAME", Name));
 87      Insert (T, Assoc ("RECIP_ADDRESS", Address));
 88      Insert
 89        (T, Assoc ("SMTP_HOST", String'(My.Config.Get (My.SMTP_Host))));
 90      Insert
 91        (T, Assoc ("SMTP_PORT", String'(My.Config.Get (My.SMTP_Port))));
 92   end Populate_Form;
 93
 94   ------------------
 95   --  Send_Email  --
 96   ------------------
 97
 98   procedure Send_Email
 99     (Address : in     String;
100      Name    : in     String;
101      T       :    out AWS.Templates.Translate_Set)
102   is
103      use AWS.Templates;
104      use Yolk.Email;
105      use Yolk.Log;
106
107      Email : Structure;
108   begin
109      if Address /= "" then
110         Composer.Add_Custom_Header (ES      => Email,
111                                     Name    => "User-Agent",
112                                     Value   => "Yolk " & Yolk.Version);
113         Composer.Send (ES           => Email,
114                        From_Address => "thomas@12boo.net",
115                        From_Name    => "Thomas Løcke",
116                        To_Address   => Address,
117                        To_Name      => Name,
118                        Subject      => "Test email",
119                        Text_Part    => "Test email from Yolk",
120                        SMTP_Server  => My.Config.Get (My.SMTP_Host),
121                        SMTP_Port    => My.Config.Get (My.SMTP_Port),
122                        Charset      => UTF8);
123
124         if Composer.Is_Send (Email) then
125            Insert (T, Assoc ("IS_SEND", True));
126            Insert (T, Assoc ("SMTP_HOST",
127              String'(My.Config.Get (My.SMTP_Host))));
128
129            Trace (Handle  => Info,
130                   Message => "Email sent to " &
131                     Address &
132                     " using " &
133                     String'(My.Config.Get (My.SMTP_Host)));
134         else
135            Insert (T, Assoc ("IS_SEND", False));
136            --  Sending failed.
137
138            Trace (Handle  => Error,
139                   Message => "Email failed to " &
140                     Address &
141                     " using " &
142                     String'(My.Config.Get (My.SMTP_Host)));
143
144            Populate_Form (Address => Address,
145                           Name    => Name,
146                           T                 => T);
147         end if;
148      else
149         Insert (T, Assoc ("IS_SEND", False));
150         --  No recipient address, so obviously we cannot send the email.
151
152         Populate_Form (Address => Address,
153                        Name    => Name,
154                        T                 => T);
155      end if;
156   end Send_Email;
157
158end View.Email;