Skip to content

Commit 849b22c

Browse files
committed
[smtp] new methods to add attachment from file and stream
1 parent 3363cb4 commit 849b22c

File tree

1 file changed

+70
-4
lines changed

1 file changed

+70
-4
lines changed

Quick.SMTP.pas

+70-4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
Unit : Quick.SMTP
66
Description : Send Emails
77
Author : Kike P�rez
8-
Version : 1.4
8+
Version : 1.5
99
Created : 12/10/2017
10-
Modified : 11/05/2021
10+
Modified : 08/09/2021
1111
1212
This file is part of QuickLib: https://github.com/exilon/QuickLib
1313
@@ -35,6 +35,7 @@ interface
3535

3636
uses
3737
Classes,
38+
Generics.Collections,
3839
SysUtils,
3940
IdGlobal,
4041
IdSMTP,
@@ -44,10 +45,21 @@ interface
4445
IdText,
4546
IdAttachment,
4647
IdAttachmentFile,
48+
IdAttachmentMemory,
4749
IdExplicitTLSClientServerBase,
4850
IdHTTP;
4951

5052
type
53+
TAttachment = class
54+
private
55+
fFilename : string;
56+
fContent : TStream;
57+
public
58+
constructor Create(const aFilename : string; const aContent : TStream);
59+
destructor Destroy; override;
60+
property Filename : string read fFilename;
61+
property Content : TStream read fContent;
62+
end;
5163

5264
TMailMessage = class
5365
private
@@ -61,7 +73,9 @@ TMailMessage = class
6173
fReplyTo : string;
6274
fBodyFromFile : Boolean;
6375
fAttachments : TStringList;
76+
fAttachmentFiles : TObjectList<TAttachment>;
6477
procedure SetBody(aValue : string);
78+
procedure SetAttachments(const Value: TStringList);
6579
public
6680
constructor Create;
6781
destructor Destroy; override;
@@ -73,7 +87,10 @@ TMailMessage = class
7387
property CC : string read fCC write fCC;
7488
property BCC : string read fBCC write fBCC;
7589
property ReplyTo : string read fReplyTo write fReplyTo;
76-
property Attachments : TStringList read fAttachments write fAttachments;
90+
property Attachments : TStringList read fAttachments write SetAttachments;
91+
property AttachmentFiles : TObjectList<TAttachment> read fAttachmentFiles;
92+
procedure AddAttachment(const aFilename : string; aStream : TStream); overload;
93+
procedure AddAttachment(const aFilename, aFilePath : string); overload;
7794
procedure AddBodyFromFile(const cFileName : string);
7895
end;
7996

@@ -107,20 +124,43 @@ constructor TMailMessage.Create;
107124
fBCC := '';
108125
fBody := '';
109126
fAttachments := TStringList.Create;
127+
fAttachmentFiles := TObjectList<TAttachment>.Create(True);
110128
end;
111129

112130
destructor TMailMessage.Destroy;
113131
begin
114132
if Assigned(fAttachments) then fAttachments.Free;
133+
if Assigned(fAttachmentFiles) then fAttachmentFiles.Free;
115134
inherited;
116135
end;
117136

137+
procedure TMailMessage.AddAttachment(const aFilename, aFilePath : string);
138+
var
139+
fs : TFileStream;
140+
begin
141+
if not FileExists(aFilePath) then raise Exception.CreateFmt('MailMessage: file "%s" not found!',[aFilename]);
142+
fs := TFileStream.Create(aFilePath,fmOpenRead);
143+
fAttachmentFiles.Add(TAttachment.Create(aFilename,fs));
144+
end;
145+
146+
procedure TMailMessage.AddAttachment(const aFilename : string; aStream : TStream);
147+
begin
148+
fAttachmentFiles.Add(TAttachment.Create(aFilename,aStream));
149+
end;
150+
118151
procedure TMailMessage.AddBodyFromFile(const cFileName: string);
119152
begin
120153
fBodyFromFile := True;
121154
fBody := cFileName;
122155
end;
123156

157+
procedure TMailMessage.SetAttachments(const Value: TStringList);
158+
begin
159+
if Assigned(fAttachments) then fAttachments.Free;
160+
161+
fAttachments := Value;
162+
end;
163+
124164
procedure TMailMessage.SetBody(aValue: string);
125165
begin
126166
fBodyFromFile := False;
@@ -198,7 +238,8 @@ function TSMTP.SendMail(aMail : TMailMessage) : Boolean;
198238
email : string;
199239
filename : string;
200240
mBody : TIdText;
201-
idattach : TIdAttachmentFile;
241+
idattach : TIdAttachment;
242+
attach : TAttachment;
202243
begin
203244
Result := False;
204245
SSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
@@ -234,6 +275,17 @@ function TSMTP.SendMail(aMail : TMailMessage) : Boolean;
234275
idattach := TIdAttachmentFile.Create(msg.MessageParts,filename);
235276
end;
236277
end;
278+
//add stream attachments if exists
279+
if aMail.AttachmentFiles.Count > 0 then
280+
begin
281+
//mBody.ContentType := 'multipart/mixed';
282+
//msg.ContentType := 'multipart/mixed';
283+
for attach in aMail.AttachmentFiles do
284+
begin
285+
idattach := TIdAttachmentMemory.Create(msg.MessageParts,attach.Content);
286+
idattach.Filename := attach.Filename;
287+
end;
288+
end;
237289

238290
//configure smtp SSL
239291
try
@@ -278,4 +330,18 @@ function TSMTP.SendMail(aMail : TMailMessage) : Boolean;
278330
end;
279331

280332

333+
{ TAttachment }
334+
335+
constructor TAttachment.Create(const aFilename: string; const aContent: TStream);
336+
begin
337+
fFilename := aFilename;
338+
fContent := aContent;
339+
end;
340+
341+
destructor TAttachment.Destroy;
342+
begin
343+
if Assigned(fContent) then fContent.Free;
344+
inherited;
345+
end;
346+
281347
end.

0 commit comments

Comments
 (0)