// http://www.fx1.net/maillink.php
//
// Demo
//
// This code detects extraordinary gaps and sends email.
//
// Change Input variables to your email settings
// Also edit Smtp Settings on MailIinit function
// ccemail and replyto can be left "" if you dont want cc/reply-to
//
// We work with realtime data (on LastBar)
//
// Import maillink functions
external: "maillink.dll", int, "MailInit",LPSTR,int,LPSTR,LPSTR;  
external: "maillink.dll", int, "MailSend",int,LPSTR,LPSTR,LPSTR,LPSTR;  
external: "maillink.dll", LPSTR, "MailErrorString",int;   
external: "maillink.dll", int, "MailAddHeader",int,LPSTR;   
external: "maillink.dll", int, "MailResetHeader",int; 
external: "maillink.dll", int, "MailClose",int; 

// Inputs
Inputs: recipient("your@email.com"),ccemail(""),senderemail("tradestation@email.
com"),replyto("noreply@email.com");

// Variable for handle must be declared intrabarpersis
Vars: intrabarpersist smtp(0);
Vars: returncode(0), intrabarpersist LastBarNumber(-1);

// First step is always to initialise our mail server
if BarNumber = 1 then
begin
  // change the parameters here to your own
  smtp = MailInit("mail.provider.com",25,"mylogin","mypassword");
  if smtp<0 then Print ("Cannot send email. Errorcode:",smtp)
  else
  begin
    // success, lets define our headers
    Print ("MailLink successfully initialised. Handle : ", smtp);
    if ccemail<>"" then     MailAddHeader(smtp, "CC: admin@wap3.net");
    if replyto<>"" then MailAddHeader(smtp,"Reply-To: "+replyto);
    MailAddHeader(smtp,"X-Symbol: "+GetSymbolName);
  end;

end; 

// start detecting on last bar (realtime) our gaps
if LastBarOnChart then
begin
if (Open > High[1] and close[1]<>High[1]) or ( Open < Low[1] and Close[1]<>Low[1]
) 
then    
  begin 
  // big gap, notify via email if mail server was successfully initialised
  // we avoid sending double emails with little trick of remembering last
  // bar we sent email.
  if smtp>0 and BarNumber<>LastBarNumber then
    begin
      returncode = MailSend(smtp,senderemail,recipient
      ,"["+GetSymbolName+"] Big Gap detected! Act now!"
      ,"Dear John,\n\nI have detected a big gap on Symbol "+GetSymbolName+"\nYou 
      should act now!");
      LastBarNumber = BarNumber;
    end;
  end;
end;
 