07-17-2024, 08:15 PM
Well after mulling this over, despite your advice to me it makes sense to actually call RCPTTo event again and this is why..
That event filters out the ok addresses.. but then in the MsgReceive event I still need to know if incoming mail is for a local account or external. The LAction variable parameter of the call to the RCPTTo is rather useful here and despite essentially duplicating the previous behaviour that variable then makes it easier to determine what to do with the incoming mail:
That event filters out the ok addresses.. but then in the MsgReceive event I still need to know if incoming mail is for a local account or external. The LAction variable parameter of the call to the RCPTTo is rather useful here and despite essentially duplicating the previous behaviour that variable then makes it easier to determine what to do with the incoming mail:
Code:
procedure TDataModule1.IdSMTPServer1RcptTo(ASender: TIdSMTPServerContext;
const AAddress: String; AParams: TStrings; var VAction: TIdRCPToReply;
var VForward: String);
var
Local: Boolean;
begin
Local := False;
//Check if address is local
Query1.Close;
Query1.SQL.Text := SQL('CheckAddressDomainIsLocal');
Query1.Prepare;
Query1.ParamByName('address').AsString := Utils.Parse('@', AAddress, 1);
Query1.ParamByName('domain').AsString := Utils.Parse('@', AAddress, 2);
Query1.Open;
with Query1 do
begin
First;
while not EOF do
begin
//Record found in database so sending to local address
Local := True;
Break;
end;
end;
if ASender.LoggedIn then
begin
if not Local then
begin
VAction := rWillForward;
end;
if Local then
begin
VAction := rAddressOk;
end;
end
else
begin
//VAction := rNoForward; - If user is/was known and has forwarding address
// or not local and no forwarding address
if not Local then
begin
VAction := rRelayDenied;
end;
if Local then
begin
VAction := rAddressOk;
end;
end;
end;Code:
procedure TDataModule1.IdSMTPServer1MsgReceive(
ASender: TIdSMTPServerContext; AMsg: TStream; var VAction: TIdDataReply);
var
I: Integer;
LAction : TIdRCPToReply;
LForward: String;
Local: Boolean;
Msg: TIdMessage;
SMTP: TIdSMTP;
begin
LAction := rRelayDenied;
SMTP := TIdSMTP.Create;
Msg := TIdMessage.Create;
Msg.LoadFromStream(AMsg);
for I := 0 to ASender.RCPTList.Count -1 do
begin
IdSMTPServer1RcptTo(ASender, ASender.RCPTList.Items[I].Address, nil, LAction, LForward);
case LAction of //Useful
rAddressOk:
begin
{TODO : Save to database}
Msg.SaveToFile(ExtractFilePath(ParamStr(0)) + 'email.eml');
end;
rWillForward:
begin
{TODO : Ssnd onwards elsewhere}
//SMTP.Send(Msg);
end;
end;
end;
VAction := dOk;
Msg.Free;
end;
If you have any better ideas.. otherwise this works for me!
