Call Centre - Email Import   


 

{This script shows importing from and email send via a web form}

{Shows using the InsertRecord function with field mapping using a StringList}

{Wide range of uses, the example show entering call centre Tickets from a web page}

{You can use page below to test example}

{http://www.development-x.com/callticket.html}

 

{Script Constants, change to suit your email settings - or demo using the ones below}

const

POP3Server = 'mail.development-x.com';

POP3UserName = 'demo@development-x.com';

POP3Password = 'demo';

 

var

CallCount: Integer;

 

function GetNextMappingIndex(CurrIndex: Integer; EmailBody: String): Integer;

var

TempList: TStringList;

x: Integer;

begin

{Function to find the end of a multi line value - (memo notes)}

Result := CurrIndex;

TempList := TStringList.create;

try

   TempList.Text := EmailBody;

   for x := (CurrIndex+1) to TempList.Count - 1 do

   begin

     if trim(TempList.Names[x]) <> '' then

     begin

       Result := x;

       exit;

       break;

     end;

     inc(Result);

   end;

finally

   TempList.free;

end;

end;

 

function RetreiveValueFromBody(EmailBody: String; FieldName: String): String;

var

TempList: TStringList;

x, i, endindex: Integer;

begin

{Function to get a value from the email body in format Name=Value}

Result := '';

TempList := TStringList.create;

try

   TempList.Text := EmailBody;

   x := TempList.IndexOfName(FieldName);

   if x = -1 then exit;

   {Multi Line Values}

   endindex := GetNextMappingIndex(x,EmailBody);

   Result := TempList.Values[TempList.Names[x]];

   if endindex > (x+1) then

   for i := (x+1) to endindex do

   if pos('MID #',TempList.Strings[i]) = 0 then {remove any unwanted text from the email body}

     Result := Result + #13 + TempList.Strings[i];

finally

   TempList.free;

end;

end;

 

procedure GetEmailRequest;

var

MsgCount: Integer;

x: Integer;

EmailSubject, EmailBody: String;

Mappings: TStringList;

begin

Mappings := TStringList.Create;

try

   {receive email using settings specified at top of script in "const" section}

   MsgCount := ReceiveEmail(POP3Server,POP3UserName,POP3Password,True);

   for x := 1 to MsgCount do

   begin

     EmailSubject :=GetEmailMessage('Subject',x);

     EmailBody := GetEmailMessage('Body',x);

     {look for a specific subject}

     if ( (trim(EmailBody) <> '') and (uppercase(EmailSubject) = 'CALLTICKET') ) then

     begin

       Mappings.Clear;

       {Fixed Values}

       Mappings.Add('CALLMETHOD=Web');

       Mappings.Add('COMPANYTYPE=Prospect');

       Mappings.Add('CALLDATE=' + DateToStr(Date));

       Mappings.Add('CALLTIME=' + timetostr(Now));

       {Values read from email}

       Mappings.Add('COMPANYNAME=' + RetreiveValueFromBody(EmailBody,'COMPANYNAME'));

       Mappings.Add('CALLNOTE=' + RetreiveValueFromBody(EmailBody,'CALLNOTE'));

       {Insert record into CALLNOTES table}

       InsertRecord('CALLNOTES',Mappings.text,True);

       inc(CallCount);

     end;

   end;

finally

   Mappings.free;

end;

end;

 

begin {Main Script Section}

CallCount := 0;

{Call function to receive email messages sent from website, mobile phone, PDA, text to email etc}

GetEmailRequest;

MessageDlg(inttostr(CallCount) + ' call requests were processed',mtInformation,mbok,0);

end.