Quick.SMTP.pas 7.4 KB

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