Quick.SMTP.pas 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. { ***************************************************************************
  2. Copyright (c) 2016-2018 Kike Pérez
  3. Unit : Quick.SMTP
  4. Description : Send Emails
  5. Author : Kike Pérez
  6. Version : 1.4
  7. Created : 12/10/2017
  8. Modified : 22/06/2018
  9. This file is part of QuickLib: https://github.com/exilon/QuickLib
  10. ***************************************************************************
  11. Licensed under the Apache License, Version 2.0 (the "License");
  12. you may not use this file except in compliance with the License.
  13. You may obtain a copy of the License at
  14. http://www.apache.org/licenses/LICENSE-2.0
  15. Unless required by applicable law or agreed to in writing, software
  16. distributed under the License is distributed on an "AS IS" BASIS,
  17. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. See the License for the specific language governing permissions and
  19. limitations under the License.
  20. *************************************************************************** }
  21. unit Quick.SMTP;
  22. interface
  23. {$i QuickLib.inc}
  24. uses
  25. Classes,
  26. SysUtils,
  27. IdGlobal,
  28. IdSMTP,
  29. IdMessage,
  30. IdReplySMTP,
  31. IdSSLOpenSSL,
  32. IdText,
  33. IdAttachment,
  34. IdAttachmentFile,
  35. IdExplicitTLSClientServerBase,
  36. IdHTTP;
  37. type
  38. TMailMessage = class
  39. private
  40. fSenderName : string;
  41. fFrom : string;
  42. fRecipient : string;
  43. fSubject : string;
  44. fBody : string;
  45. fCC : string;
  46. fBCC : string;
  47. fBodyFromFile : Boolean;
  48. fAttachments : TStringList;
  49. procedure SetBody(aValue : string);
  50. public
  51. constructor Create;
  52. destructor Destroy; override;
  53. property SenderName : string read fSenderName write fSenderName;
  54. property From : string read fFrom write fFrom;
  55. property Recipient : string read fRecipient write fRecipient;
  56. property Subject : string read fSubject write fSubject;
  57. property Body : string read fBody write SetBody;
  58. property CC : string read fCC write fCC;
  59. property BCC : string read fBCC write fBCC;
  60. property Attachments : TStringList read fAttachments write fAttachments;
  61. procedure AddBodyFromFile(const cFileName : string);
  62. end;
  63. TSMTP = class(TIdSMTP)
  64. private
  65. fServerAuth : Boolean;
  66. fUseSSL : Boolean;
  67. fMail : TMailMessage;
  68. public
  69. constructor Create; overload;
  70. constructor Create(const cHost : string; cPort : Integer; cUseSSL : Boolean = False); overload;
  71. destructor Destroy; override;
  72. property ServerAuth : Boolean read fServerAuth write fServerAuth;
  73. property UseSSL: Boolean read fUseSSL write fUseSSL;
  74. property Mail : TMailMessage read fMail write fMail;
  75. function SendMail: Boolean; overload;
  76. function SendMail(aMail : TMailMessage) : Boolean; overload;
  77. function SendEmail(const aFromName,aSubject,aTo,aCC,aBC,aBody : string) : Boolean; overload;
  78. end;
  79. implementation
  80. { TMailMessage }
  81. constructor TMailMessage.Create;
  82. begin
  83. fCC := '';
  84. fBCC := '';
  85. fBody := '';
  86. fAttachments := TStringList.Create;
  87. end;
  88. destructor TMailMessage.Destroy;
  89. begin
  90. if Assigned(fAttachments) then fAttachments.Free;
  91. inherited;
  92. end;
  93. procedure TMailMessage.AddBodyFromFile(const cFileName: string);
  94. begin
  95. fBodyFromFile := True;
  96. fBody := cFileName;
  97. end;
  98. procedure TMailMessage.SetBody(aValue: string);
  99. begin
  100. fBodyFromFile := False;
  101. fBody := aValue;
  102. end;
  103. { TSMTP }
  104. constructor TSMTP.Create;
  105. begin
  106. inherited Create;
  107. fMail := TMailMessage.Create;
  108. Port := 25;
  109. UseTLS := TIdUseTLS.utNoTLSSupport;
  110. fUseSSL := False;
  111. end;
  112. constructor TSMTP.Create(const cHost : string; cPort : Integer; cUseSSL : Boolean = False);
  113. begin
  114. Create;
  115. Host := cHost;
  116. Port := cPort;
  117. fUseSSL := cUseSSL;
  118. end;
  119. destructor TSMTP.Destroy;
  120. begin
  121. if Assigned(fMail) then fMail.Free;
  122. inherited;
  123. end;
  124. function TSMTP.SendEmail(const aFrom,aFromName,aSubject,aTo,aCC,aBC,aBody : string) : Boolean;
  125. var
  126. mail : TMailMessage;
  127. begin
  128. mail := TMailMessage.Create;
  129. try
  130. Mail.From := aFrom;
  131. Mail.SenderName := aFromName;
  132. Mail.Subject := aSubject;
  133. Mail.Body := aBody;
  134. Mail.Recipient := aTo;
  135. Mail.CC := aCC;
  136. Mail.BCC := aBC;
  137. Result := Self.SendMail(mail);
  138. finally
  139. mail.Free;
  140. end;
  141. end;
  142. function TSMTP.SendMail: Boolean;
  143. begin
  144. Result := SendMail(fMail);
  145. end;
  146. function TSMTP.SendMail(aMail : TMailMessage) : Boolean;
  147. var
  148. msg : TIdMessage;
  149. SSLHandler : TIdSSLIOHandlerSocketOpenSSL;
  150. email : string;
  151. filename : string;
  152. mBody : TIdText;
  153. idattach : TIdAttachmentFile;
  154. begin
  155. Result := False;
  156. SSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
  157. try
  158. msg := TIdMessage.Create(nil);
  159. try
  160. //create mail msg
  161. idattach := nil;
  162. mBody := nil;
  163. msg.From.Address := aMail.From;
  164. if aMail.SenderName <> '' then msg.From.Name := aMail.SenderName;
  165. msg.Subject := aMail.Subject;
  166. for email in aMail.Recipient.Split([',',';']) do msg.Recipients.Add.Address := email;
  167. for email in aMail.CC.Split([',',';']) do msg.CCList.Add.Address := email;
  168. for email in aMail.BCC.Split([',',';']) do msg.BCCList.Add.Address := email;
  169. if aMail.fBodyFromFile then
  170. begin
  171. msg.Body.LoadFromFile(aMail.Body);
  172. end
  173. else
  174. begin
  175. mBody := TIdText.Create(msg.MessageParts);
  176. mBody.ContentType := 'text/html';
  177. mBody.Body.Text := aMail.Body;
  178. end;
  179. //add attachements if exists
  180. if aMail.Attachments.Count > 0 then
  181. begin
  182. for filename in aMail.Attachments do
  183. begin
  184. idattach := TIdAttachmentFile.Create(msg.MessageParts,filename);
  185. end;
  186. end;
  187. //configure smtp SSL
  188. try
  189. if fUseSSL then
  190. begin
  191. Self.IOHandler := SSLHandler;
  192. SSLHandler.MaxLineAction := maException;
  193. SSLHandler.SSLOptions.Method := sslvTLSv1_2;
  194. SSLHandler.SSLOptions.Mode := sslmUnassigned;
  195. SSLHandler.SSLOptions.VerifyMode := [];
  196. SSLHandler.SSLOptions.VerifyDepth := 0;
  197. //Self.UseTLS := utUseExplicitTLS;
  198. end;
  199. //server auth
  200. if ServerAuth then Self.AuthType := TIdSMTPAuthenticationType.satDefault;
  201. Self.Port := fPort;
  202. except
  203. on E : Exception do raise Exception.Create(Format('[%s] : %s',[Self.ClassName,e.Message]));
  204. end;
  205. //send email
  206. try
  207. Self.Connect;
  208. if Self.Connected then
  209. begin
  210. Self.Send(msg);
  211. Self.Disconnect(False);
  212. Result := True;
  213. end;
  214. except
  215. on E : Exception do raise Exception.Create(Format('[%s] : %s',[Self.ClassName,e.Message]));
  216. end;
  217. finally
  218. if Assigned(idattach) then idattach.Free;
  219. if Assigned(mBody) then mBody.Free;
  220. msg.Free;
  221. end;
  222. finally
  223. SSLHandler.Free;
  224. end;
  225. end;
  226. end.