Quick.SMTP.pas 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 : 02/09/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 aFromEmail,aFromName,aSubject,aTo,aCC,aBC,aBody : string) : Boolean; overload;
  78. function SendEmail(const aFromName,aSubject,aTo,aCC,aBC,aBody : string) : Boolean; overload;
  79. end;
  80. implementation
  81. { TMailMessage }
  82. constructor TMailMessage.Create;
  83. begin
  84. fCC := '';
  85. fBCC := '';
  86. fBody := '';
  87. fAttachments := TStringList.Create;
  88. end;
  89. destructor TMailMessage.Destroy;
  90. begin
  91. if Assigned(fAttachments) then fAttachments.Free;
  92. inherited;
  93. end;
  94. procedure TMailMessage.AddBodyFromFile(const cFileName: string);
  95. begin
  96. fBodyFromFile := True;
  97. fBody := cFileName;
  98. end;
  99. procedure TMailMessage.SetBody(aValue: string);
  100. begin
  101. fBodyFromFile := False;
  102. fBody := aValue;
  103. end;
  104. { TSMTP }
  105. constructor TSMTP.Create;
  106. begin
  107. inherited Create;
  108. fMail := TMailMessage.Create;
  109. Port := 25;
  110. UseTLS := TIdUseTLS.utNoTLSSupport;
  111. fUseSSL := False;
  112. end;
  113. constructor TSMTP.Create(const cHost : string; cPort : Integer; cUseSSL : Boolean = False);
  114. begin
  115. Create;
  116. Host := cHost;
  117. Port := cPort;
  118. fUseSSL := cUseSSL;
  119. end;
  120. destructor TSMTP.Destroy;
  121. begin
  122. if Assigned(fMail) then fMail.Free;
  123. inherited;
  124. end;
  125. function TSMTP.SendEmail(const aFromEmail, aFromName, aSubject, aTo, aCC, aBC, aBody: string): Boolean;
  126. begin
  127. fMail.From := aFromEmail;
  128. Result := SendEmail(aFromName,aSubject,aTo,aCC,aBC,aBody);
  129. end;
  130. function TSMTP.SendEmail(const aFromName,aSubject,aTo,aCC,aBC,aBody : string) : Boolean;
  131. var
  132. mail : TMailMessage;
  133. begin
  134. if fMail.From.IsEmpty then raise Exception.Create('Email sender not specified!');
  135. mail := TMailMessage.Create;
  136. try
  137. Mail.From := fMail.From;
  138. if aFromName.IsEmpty then Mail.SenderName := fMail.From
  139. else Mail.SenderName := aFromName;
  140. Mail.Subject := aSubject;
  141. Mail.Body := aBody;
  142. Mail.Recipient := aTo;
  143. Mail.CC := aCC;
  144. Mail.BCC := aBC;
  145. Result := Self.SendMail(mail);
  146. finally
  147. mail.Free;
  148. end;
  149. end;
  150. function TSMTP.SendMail: Boolean;
  151. begin
  152. Result := SendMail(fMail);
  153. end;
  154. function TSMTP.SendMail(aMail : TMailMessage) : Boolean;
  155. var
  156. msg : TIdMessage;
  157. SSLHandler : TIdSSLIOHandlerSocketOpenSSL;
  158. email : string;
  159. filename : string;
  160. mBody : TIdText;
  161. idattach : TIdAttachmentFile;
  162. begin
  163. Result := False;
  164. SSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
  165. try
  166. msg := TIdMessage.Create(nil);
  167. try
  168. //create mail msg
  169. idattach := nil;
  170. mBody := nil;
  171. msg.From.Address := aMail.From;
  172. if aMail.SenderName <> '' then msg.From.Name := aMail.SenderName;
  173. msg.Subject := aMail.Subject;
  174. for email in aMail.Recipient.Split([',',';']) do msg.Recipients.Add.Address := email;
  175. for email in aMail.CC.Split([',',';']) do msg.CCList.Add.Address := email;
  176. for email in aMail.BCC.Split([',',';']) do msg.BCCList.Add.Address := email;
  177. if aMail.fBodyFromFile then
  178. begin
  179. msg.Body.LoadFromFile(aMail.Body);
  180. end
  181. else
  182. begin
  183. mBody := TIdText.Create(msg.MessageParts);
  184. mBody.ContentType := 'text/html';
  185. mBody.Body.Text := aMail.Body;
  186. end;
  187. //add attachements if exists
  188. if aMail.Attachments.Count > 0 then
  189. begin
  190. for filename in aMail.Attachments do
  191. begin
  192. idattach := TIdAttachmentFile.Create(msg.MessageParts,filename);
  193. end;
  194. end;
  195. //configure smtp SSL
  196. try
  197. if fUseSSL then
  198. begin
  199. Self.IOHandler := SSLHandler;
  200. SSLHandler.MaxLineAction := maException;
  201. SSLHandler.SSLOptions.Method := sslvTLSv1_2;
  202. SSLHandler.SSLOptions.Mode := sslmUnassigned;
  203. SSLHandler.SSLOptions.VerifyMode := [];
  204. SSLHandler.SSLOptions.VerifyDepth := 0;
  205. //Self.UseTLS := utUseExplicitTLS;
  206. end;
  207. //server auth
  208. if ServerAuth then Self.AuthType := TIdSMTPAuthenticationType.satDefault;
  209. Self.Port := fPort;
  210. except
  211. on E : Exception do raise Exception.Create(Format('[%s] : %s',[Self.ClassName,e.Message]));
  212. end;
  213. //send email
  214. try
  215. Self.Connect;
  216. if Self.Connected then
  217. begin
  218. Self.Send(msg);
  219. Self.Disconnect(False);
  220. Result := True;
  221. end;
  222. except
  223. on E : Exception do raise Exception.Create(Format('[%s] : %s',[Self.ClassName,e.Message]));
  224. end;
  225. finally
  226. if Assigned(idattach) then idattach.Free;
  227. if Assigned(mBody) then mBody.Free;
  228. msg.Free;
  229. end;
  230. finally
  231. SSLHandler.Free;
  232. end;
  233. end;
  234. end.