/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

  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. with Ada.Strings.Fixed;
  18. with AWS.Parameters;
  19. with AWS.Templates;
  20. with Yolk.Email.Composer;
  21. with Yolk.Log;
  22. package body View.Email is
  23. procedure Populate_Form
  24. (Address : in String;
  25. Name : in String;
  26. T : out AWS.Templates.Translate_Set);
  27. -- Insert the form data associations into the T translate set.
  28. procedure Send_Email
  29. (Address : in String;
  30. Name : in String;
  31. T : out AWS.Templates.Translate_Set);
  32. -- Construct and send an email to recipient.
  33. ---------------
  34. -- Generate --
  35. ---------------
  36. function Generate
  37. (Request : in AWS.Status.Data)
  38. return AWS.Response.Data
  39. is
  40. use AWS.Templates;
  41. use Ada.Strings;
  42. P : constant AWS.Parameters.List :=
  43. AWS.Status.Parameters (Request);
  44. Name : constant String :=
  45. Fixed.Trim (P.Get ("recip_name"), Both);
  46. Address : constant String :=
  47. Fixed.Trim (P.Get ("recip_address"), Both);
  48. T : Translate_Set;
  49. begin
  50. if Name'Length > 0
  51. and then Address'Length > 0
  52. then
  53. Send_Email (Address => Address,
  54. Name => Name,
  55. T => T);
  56. else
  57. Populate_Form (Address => "yolk@mailinator.com",
  58. Name => "Zaphod Beeblebrox",
  59. T => T);
  60. end if;
  61. return Build_Response
  62. (Status_Data => Request,
  63. Template_File => My.Config.Get (My.Template_Email),
  64. Translations => T);
  65. end Generate;
  66. ---------------------
  67. -- Populate_Form --
  68. ---------------------
  69. procedure Populate_Form
  70. (Address : in String;
  71. Name : in String;
  72. T : out AWS.Templates.Translate_Set)
  73. is
  74. use AWS.Templates;
  75. begin
  76. Insert (T, Assoc ("RECIP_NAME", Name));
  77. Insert (T, Assoc ("RECIP_ADDRESS", Address));
  78. Insert
  79. (T, Assoc ("SMTP_HOST", String'(My.Config.Get (My.SMTP_Host))));
  80. Insert
  81. (T, Assoc ("SMTP_PORT", String'(My.Config.Get (My.SMTP_Port))));
  82. end Populate_Form;
  83. ------------------
  84. -- Send_Email --
  85. ------------------
  86. procedure Send_Email
  87. (Address : in String;
  88. Name : in String;
  89. T : out AWS.Templates.Translate_Set)
  90. is
  91. use AWS.Templates;
  92. use Yolk.Email;
  93. use Yolk.Log;
  94. Email : Structure;
  95. begin
  96. if Address /= "" then
  97. Composer.Add_Custom_Header (ES => Email,
  98. Name => "User-Agent",
  99. Value => "Yolk " & Yolk.Version);
  100. Composer.Send (ES => Email,
  101. From_Address => "thomas@12boo.net",
  102. From_Name => "Thomas Løcke",
  103. To_Address => Address,
  104. To_Name => Name,
  105. Subject => "Test email",
  106. Text_Part => "Test email from Yolk",
  107. SMTP_Server => My.Config.Get (My.SMTP_Host),
  108. SMTP_Port => My.Config.Get (My.SMTP_Port),
  109. Charset => UTF8);
  110. if Composer.Is_Send (Email) then
  111. Insert (T, Assoc ("IS_SEND", True));
  112. Insert (T, Assoc ("SMTP_HOST",
  113. String'(My.Config.Get (My.SMTP_Host))));
  114. Trace (Handle => Info,
  115. Message => "Email sent to " &
  116. Address &
  117. " using " &
  118. String'(My.Config.Get (My.SMTP_Host)));
  119. else
  120. Insert (T, Assoc ("IS_SEND", False));
  121. -- Sending failed.
  122. Trace (Handle => Error,
  123. Message => "Email failed to " &
  124. Address &
  125. " using " &
  126. String'(My.Config.Get (My.SMTP_Host)));
  127. Populate_Form (Address => Address,
  128. Name => Name,
  129. T => T);
  130. end if;
  131. else
  132. Insert (T, Assoc ("IS_SEND", False));
  133. -- No recipient address, so obviously we cannot send the email.
  134. Populate_Form (Address => Address,
  135. Name => Name,
  136. T => T);
  137. end if;
  138. end Send_Email;
  139. end View.Email;