procedure TPrintBaseForm.PsRBExportMasterControlEMail(
Sender: TPsRBExportDevice; EMailInfo: TPsRBEMailInfo;
var Continue: Boolean);
var
EmailParamFrm: TEmailParamsForm;
res: TModalResult;
ExpMstCtrl: TPsRBExportMasterControl;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function GetAddressString(AText: string): string;
begin
Result := Trim(AText);
Result := StringReplace(Result, CRLF, ';', [rfReplaceAll]);
if (Result <> '') and (Result[Length(Result)] = ';') then
System.Delete(Result, Length(Result), 1);
end;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
procedure DoSMTPCreate;
begin
if (not ConnectOnceSendMult) or (fIdSMTP=nil) then
begin
fIdSMTP:=TIdSMTP.Create(Self);
//fIdSMTP.OnConnected:=ExpMstCtrl.OnEMailConnected;
//fIdSMTP.OnDisconnected:=ExpMstCtrl.OnEMailDisConnected;
//fIdSMTP.OnStatus:=ExpMstCtrl.OnEMailStatus;
fIdSMTP.Host:=EMailInfo.Host;
fIdSMTP.UserName:=EMailInfo.UserID;
fIdSMTP.Password:=EMailInfo.Password;
fIdSMTP.AuthType:=satNone;
end;
end;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
procedure DoSMTPConnect;
begin
if (not ConnectOnceSendMult) or ((fIdSMTP<>nil) and (not fIdSMTP.Connected)) then
fIdSMTP.Connect;
end;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
procedure DoSMTPDisconnect;
begin
if (not ConnectOnceSendMult) or (IsLastInSendMult and (fIdSMTP<>nil) and fIdSMTP.Connected) then
fIdSMTP.Disconnect;
end;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
procedure DoSMTPFree;
begin
if (not ConnectOnceSendMult) or (IsLastInSendMult and (fIdSMTP<>nil)) then
FreeAndNil(fIdSMTP);
end;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
procedure SendEmail;
var
IdMessage: TIdMessage;
IdAttachment: TIdAttachmentFile;
i: integer;
begin
ExpMstCtrl:=PsRBExportMasterControl;
IdMessage:=TIdMessage.Create(Self);
try
DoSMTPCreate;
with IdMessage do
begin
for i:=0 to EMailInfo.Attachments.Count-1 do
IdAttachment:=TIDAttachmentFile.Create(MessageParts,EmailInfo.Attachments[i]);
Body.Assign(EMailInfo.Body);
Date:=EMailInfo.Date;
From.Name:=EMailInfo.FromName;
From.Address:=EMailInfo.FromAddress;
ReplyTo.EMailAddresses:=EMailInfo.ReplyTo;
Subject:=EMailInfo.Subject;
Recipients.EMailAddresses:=GetAddressString(EMailInfo.RecipientList.Text);
CCList.EMailAddresses:=GetAddressString(EMailInfo.CCList.Text);
BCCList.EMailAddresses:=GetAddressString(EMailInfo.BCCList.Text);
end;
try
DoSMTPConnect;
fIdSMTP.Send(IdMessage);
//if Assigned(ExpMstCtrl.OnEMailSuccess) then
// ExpMstCtrl.OnEMailSuccess(ExpMstCtrl);
EmailSent:=True;
DisplayMailProgress(' Email Sent Successfully',True);
except
on E: Exception do
begin
//if Assigned(ExpMstCtrl.OnEMailError) then
// ExpMstCtrl.OnEmailError(Sender,E.Message)
//else
DisplayMailProgress(' Error: '+E.Message,True);
MessageDlg(E.Message,mtError,[mbOK],0);
Continue:=False;
end;
end;
finally
DoSMTPDisconnect;
DoSMTPFree;
IdMessage.Free;
end;
end;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
begin
if EmailDialogsEnabled then
begin
EmailParamFrm:=TEmailParamsForm.Create(Self,nil);
with EmailParamFrm, EMailInfo, TransPostHelper.EmailTemplatesTable do
try
edFromName.Text:=FieldByName('FromName').AsString;
edFromAddr.Text:=FieldByName('FromAddress').AsString;
edTo.Text:=EmailRecipients;
if (EmailCCRecipients<>'') then
edCc.Text:=EmailCCRecipients
else
edCc.Text:=FieldByName('CC').AsString;
if (EmailBCCRecipients<>'') then
edBcc.Text:=EmailBCCRecipients
else
edBcc.Text:=FieldByName('BCC').AsString;
if (EmailSubject<>'') then
edSubject.Text:=EmailSubject
else
edSubject.Text:=GetEmailSubject(FieldByName('Subject').AsString);
if (EmailBody<>'') then
memoBody.Lines.Text:=EmailBody
else
memoBody.Lines.Text:=FieldByName('Body').AsString;
if EmailAccount<>'' then
Caption:=Caption+' - '+EmailAccount;
res:=ShowModal;
if res=mrYes then
begin
Host := FieldByName('Host').AsString; //DisplayMailProgress('Host='+Host);
UserID := FieldByName('UserName').AsString; //DisplayMailProgress('UserId='+UserID);
Password := FieldByName('Password').AsString; //DisplayMailProgress('Password='+Password);
Body.Text:=memoBody.Lines.Text; //DisplayMailProgress('Body='+Body.Text);
Date := Now; //DisplayMailProgress('Date='+DateToStr(Date));
FromAddress := edFromAddr.Text; //DisplayMailProgress('FromAddress='+FromAddress);
FromName := edFromName.Text; //DisplayMailProgress('FromName='+FromName);
ReplyTo := FieldByName('ReplyTo').AsString; //DisplayMailProgress('ReplyTo='+ReplyTo);
Subject := edSubject.Text; //DisplayMailProgress('Subject='+Subject);
RecipientList.Commatext:=edTo.Text; //DisplayMailProgress('Recipient='+RecipientList.Commatext);
EmailRecipients:=edTo.Text;
EmailSubject:=edSubject.Text;
EmailBody:=memoBody.Lines.Text;
EmailCCRecipients:=edCc.Text;
EmailBCCRecipients:=edBcc.Text;
CCList.Add(edCc.Text);
BCCList.Add(edBcc.Text);
DeleteFilesAfterEMail:=True;
if not ReportSettings.IncludeAttachment then
Attachments.Clear;
end
else if res=mrNo then
begin
EMailInfo.CancelEMail:=True;
DisplayMailProgress('Email is NOT sent - user intervention',True);
// if SMProgForm<>nil then
// SMProgForm.UpdateProgress('Email is NOT sent - user intervention',True)
// else if SMProgDetForm<>nil then
// SMProgDetForm.UpdateProgress('Email is NOT sent - user intervention',True,False);
end
else if res=mrAbort then
begin
EMailInfo.CancelEMail:=True;
Continue:=False;
DisplayMailProgress('Email is NOT sent - user intervention',True);
DisplayMailProgress('Emailing process is stopped - user intervention',False);
// if SMProgForm<>nil then
// begin
// SMProgForm.UpdateProgress('Email is NOT sent - user intervention',True);
// SMProgForm.UpdateProgress('Emailing process is stopped - user intervention',False);
// end
// else if SMProgDetForm<>nil then
// begin
// SMProgDetForm.UpdateProgress('Email is NOT sent - user intervention',True,False);
// SMProgDetForm.UpdateProgress('Emailing process is stopped - user intervention',False,False);
// end;
end;
finally
EmailParamFrm.Free;
end;
end
else
begin
with EMailInfo, TransPostHelper.EmailTemplatesTable do
begin
Host := FieldByName('Host').AsString;
UserID := FieldByName('UserName').AsString;
Password := FieldByName('Password').AsString;
if (EmailBody<>'') then
Body.Text:=EmailBody
else
Body.Text:=FieldByName('Body').AsString;
Date := Now;
FromAddress := FieldByName('FromAddress').AsString;
FromName := FieldByName('FromName').AsString;
ReplyTo := FieldByName('ReplyTo').AsString;
if (EmailSubject<>'') then
Subject:=EmailSubject
else
Subject:=GetEmailSubject(FieldByName('Subject').AsString);
RecipientList.CommaText:=EmailRecipients;
if (EmailCCRecipients<>'') then
CCList.CommaText:=EmailCCRecipients
else
CCList.CommaText:=FieldByName('CC').AsString;
if (EmailBCCRecipients<>'') then
BCCList.CommaText:=EmailBCCRecipients
else
BCCList.CommaText:=FieldByName('BCC').AsString;
DeleteFilesAfterEMail:=True;
if not ReportSettings.IncludeAttachment then
Attachments.Clear;
end;
end;
if not EMailInfo.CancelEMail then
begin
SendEmail;
EMailInfo.CancelEMail:=True; // This is to stop the default email sending happening
end;
end;
|