PageRenderTime 23ms CodeModel.GetById 13ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/src/yolk-email.ads

http://github.com/ThomasLocke/yolk
Ada | 228 lines | 104 code | 25 blank | 99 comment | 0 complexity | fa6703910599ca38331bdf9abbdf7b2b MD5 | raw file
  1-------------------------------------------------------------------------------
  2--                                                                           --
  3--                   Copyright (C) 2010-, Thomas Løcke                   --
  4--                                                                           --
  5--  This library is free software;  you can redistribute it and/or modify    --
  6--  it 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--                                                                           --
 12--  As a special exception under Section 7 of GPL version 3, you are         --
 13--  granted additional permissions described in the GCC Runtime Library      --
 14--  Exception, version 3.1, as published by the Free Software Foundation.    --
 15--                                                                           --
 16--  You should have received a copy of the GNU General Public License and    --
 17--  a copy of the GCC Runtime Library Exception along with this program;     --
 18--  see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
 19--  <http://www.gnu.org/licenses/>.                                          --
 20--                                                                           --
 21-------------------------------------------------------------------------------
 22
 23with Ada.Containers.Vectors;
 24with Ada.Strings.Unbounded;
 25with GNATCOLL.Email;
 26
 27package Yolk.Email is
 28
 29   Attachment_File_Not_Found        : exception;
 30   --  Is raised if a file attachment is not found.
 31   No_Address_Set                   : exception;
 32   --  Is raised if the address component is missing in an Email_Data record.
 33   No_Sender_Set_With_Multiple_From : exception;
 34   --  Is raised when an email contains multiple From headers but no Sender
 35   --  header, as per RFC-5322, 3.6.2. http://tools.ietf.org/html/rfc5322
 36   No_SMTP_Host_Set                 : exception;
 37   --  Is raised if the SMTP host list is empty.
 38
 39   type Character_Set is (US_ASCII,
 40                          ISO_8859_1,
 41                          ISO_8859_2,
 42                          ISO_8859_3,
 43                          ISO_8859_4,
 44                          ISO_8859_9,
 45                          ISO_8859_10,
 46                          ISO_8859_13,
 47                          ISO_8859_14,
 48                          ISO_8859_15,
 49                          Windows_1252,
 50                          UTF8);
 51   --  The available character sets. We try to provide the same character sets
 52   --  as defined in gnatcoll-email.ads.
 53
 54   type Recipient_Kind is (Bcc, Cc, To);
 55   --  The kind of recipient, when adding a new recipient to an email.
 56
 57   type Structure is private;
 58   --  The email structure. This type holds all the information needed to build
 59   --  a proper email.
 60
 61private
 62
 63   use Ada.Containers;
 64   use Ada.Strings.Unbounded;
 65
 66   function U
 67     (S : in String)
 68      return Unbounded_String
 69      renames To_Unbounded_String;
 70
 71   type Attachment_Data is
 72      record
 73         Charset      : Character_Set := US_ASCII;
 74         Path_To_File : Unbounded_String;
 75      end record;
 76
 77   type Email_Data is
 78      record
 79         Address : Unbounded_String;
 80         Charset : Character_Set  := US_ASCII;
 81         Name    : Unbounded_String;
 82      end record;
 83
 84   type Email_Kind is (Text,
 85                       Text_With_Attachment,
 86                       Text_And_HTML,
 87                       Text_And_HTML_With_Attachment);
 88
 89   type Header_Data is
 90      record
 91         Charset : Character_Set := US_ASCII;
 92         Name    : Unbounded_String;
 93         Value   : Unbounded_String;
 94      end record;
 95
 96   type SMTP_Server is
 97      record
 98         Host : Unbounded_String;
 99         Port : Positive;
100      end record;
101
102   type Subject_Data is
103      record
104         Content : Unbounded_String;
105         Charset : Character_Set := US_ASCII;
106      end record;
107
108   type Text_Data is
109      record
110         Content : Unbounded_String;
111         Charset : Character_Set := US_ASCII;
112      end record;
113   --  This type is used for both text and HTML parts, so the "Text" part of
114   --  Text_Data simply refers to the fact that both text and HTML parts are
115   --  essentially plain text data.
116
117   package Attachments_Container is new Vectors (Positive, Attachment_Data);
118   package Custom_Headers_Container is new Vectors (Positive, Header_Data);
119   package Email_Data_Container is new Vectors (Positive, Email_Data);
120   package SMTP_Servers_Container is new Vectors (Positive, SMTP_Server);
121
122   type Structure is
123      record
124         Attachment_List  : Attachments_Container.Vector;
125         Bcc_List         : Email_Data_Container.Vector;
126         Cc_List          : Email_Data_Container.Vector;
127         Composed_Message : GNATCOLL.Email.Message;
128         Custom_Headers   : Custom_Headers_Container.Vector;
129         Email_Is_Sent    : Boolean := False;
130         From_List        : Email_Data_Container.Vector;
131         Has_Attachment   : Boolean := False;
132         Has_HTML_Part    : Boolean := False;
133         Has_Text_Part    : Boolean := False;
134         HTML_Part        : Text_Data;
135         Reply_To_List    : Email_Data_Container.Vector;
136         Sender           : Email_Data;
137         SMTP_List        : SMTP_Servers_Container.Vector;
138         Subject          : Subject_Data;
139         Text_Part        : Text_Data;
140         To_List          : Email_Data_Container.Vector;
141         Type_Of_Email    : Email_Kind;
142      end record;
143   --  The type used to hold describe an email.
144   --    Attachment_List:
145   --       A list of Attachment_Data records. The validity of the Path_To_File
146   --       component is checked when it is converted into a GNATcoll Virtual
147   --       file.
148   --    Bcc_List:
149   --       A list of Email_Data records. These are collapsed into a single
150   --       Bcc: header when Send is called, and only then do we check if each
151   --       element is valid.
152   --    Cc_List:
153   --       A list of Email_Data records. These are collapsed into a single Cc:
154   --       header when Send is called, and only then do we check if each
155   --       element is valid.
156   --    Composed_Message:
157   --       The complete email in GNATCOLL.Email.Message format.
158   --    Custom_Headers:
159   --       A list of custom headers.
160   --    Email_Is_Send:
161   --       Is set to True if we succeed in sending the email.
162   --    From_List:
163   --       A list of Email_Data records. These are collapsed into a single
164   --       From: header when Send is called, and only then do we check if each
165   --       element is valid.
166   --    Has_Attachment:
167   --       Is set to True if an attachment is added to the email.
168   --    Has_HTML_Part:
169   --       Is set to True if a HTML part is added to the email.
170   --    Has_Text_Part:
171   --       Is set to True if a Text part is added to the email.
172   --    HTML_Part:
173   --       The HTML part of a multipart/alternative email.
174   --    Reply_To_List:
175   --       List of Email_Data records. These are collapsed into a single
176   --       Reply-To: header when Send is called, and only then do we check if
177   --       each element is valid.
178   --    Sender:
179   --       If From_List contains multiple elements, then a Sender: header is
180   --       required as per RFC 5322 3.6.2. This header is build from the value
181   --       of Sender.
182   --    SMTP_List:
183   --       List of SMTP servers to try when sending the email. The first one
184   --       added to the list, is the first one tried. The system will keep
185   --       going down the list, until it either succeeds in sending the email
186   --       or until it runs out of SMTP servers to try.
187   --    Status:
188   --       The status code and message from the SMTP session.
189   --    Subject:
190   --       From this we build the Subject: header.
191   --    Text_Part:
192   --       The text/plain part of an email.
193   --    To_List:
194   --       List of Email_Data records. These are collapsed into a single To:
195   --       header when send is called, and only then do we check if each
196   --       element is valid.
197   --    Type_Of_Email:
198   --       The kind of email we're dealing with.
199
200   procedure Generate_Text_And_HTML_Email
201     (ES : in out Structure);
202   --  Generate a text and HTML email using the GNATcoll email facilities.
203
204   procedure Generate_Text_With_Attachment_Email
205     (ES : in out Structure);
206   --  Generate a text email with attachment(s) using the GNATcoll email
207   --  facilities.
208
209   procedure Generate_Text_Email
210     (ES : in out Structure);
211   --  Generate a text email using the GNATcoll email facilities.
212
213   procedure Generate_Text_And_HTML_With_Attachment_Email
214     (ES : in out Structure);
215   --  Generate a text and HTML email with attachment(s) using the GNATcoll
216   --  email facilities.
217
218   function Get_Charset
219     (Charset : in Character_Set)
220      return String;
221   --  Return the GNATcoll.Email character set string constant that is
222   --  equivalent to the given Email.Character_Set enum.
223
224   procedure Set_Type_Of_Email
225     (ES : in out Structure);
226   --  Figure out the kind of email ES is.
227
228end Yolk.Email;