Quick.SMTP.pas 7.8 KB

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