
#include "MailReact.h"
#include "sfl.h"
#include "sflmail.h"


int start_sending()
{
	sock_init();

//  int iOld_ip_nonblock = ip_nonblock;
/*  Make sure we block on socket accesses                                 */
//   ip_nonblock = FALSE;

   SMTP	smtp;
   memset(&smtp,0,sizeof(smtp));

   smtp.strSmtpServer = "";
   smtp.strMessageBody = "";
   smtp.strSubject = "";
   smtp.strSenderUserId = "";
   smtp.strFullSenderUserId = "";
   smtp.strDestUserIds = "";
   smtp.strFullDestUserIds = "";
   smtp.strCcUserIds = "";
   smtp.strFullCcUserIds = "";
   smtp.strBccUserIds = "";
   smtp.strFullBccUserIds = "";
   smtp.strRetPathUserId = "";
   smtp.strRrcpUserId = "";
   smtp.strMsgComment = "";
   smtp.strMailerName = "";
   smtp.strBinFiles = "";
   smtp.strTxtFiles = "";
   smtp.connect_retry_cnt = 3;
   smtp.retry_wait_time = 0;
   smtp.debug = 1;

 	char smtpserver[50];
	strcpy(smtpserver,SMTPval->value());
	char senderuserid[50];
	strcpy(senderuserid,sender->value());
	char subjectval[255];
	strcpy(subjectval,subject->value());
	char portnumber[10];
	strcpy(portnumber,port->value());

	char mailscon[10];
	strcpy(mailscon,mailsconnect->value());


	char xmailer[]="MailReactor";
	smtp.strMailerName=xmailer;

		char delayms[10];
	strcpy(delayms,delay->value());


	char *messagebody=(char *)mem_alloc(b2->Length()+1);
	char *receiverslist=(char *)mem_alloc(b->Length()+1);
	
	smtp.strSmtpServer=smtpserver;
	smtp.strSenderUserId=senderuserid;
	smtp.strSubject=subjectval;
	strcpy(receiverslist,b->GetAll());
	strcpy(messagebody,b2->GetAll());

	smtp.strMessageBody=messagebody;
	smtp.strDestUserIds=receiverslist;


	char mesfile[255];
	strcpy(mesfile,"output.txt");
	smtp.strDebugFile=mesfile;

   /* Open up the SMTP port (25 most of the time). */
//	char errorstring[255];
		
	/*			   sock_t iSocket;
				    ip_nonblock = FALSE;
   iSocket = connect_socket (smtp.strSmtpServer,
                             portnumber, "tcp", NULL,
                             smtp.connect_retry_cnt,
                             smtp.retry_wait_time);
   if(iSocket==INVALID_SOCKET) MessageBox(NULL,"fsdfsd",0,0);
//   sprintf(errorstring,"error: %d",socket_error(iSocket)); */
	 Abort->activate();
if(!smtp_send_mail_ex (&smtp,portnumber, atoi(mailscon),atoi(delayms)))
{ 
log_text(syslog,"All done.");
}
else
{
	char buffer[513];
	sprintf(buffer,"Something went wrong at %d.",(int)progress->value());
log_text(syslog,buffer);

}
	 Abort->deactivate();

//smtp_send_mail_ex2 (&smtp);

   /*
      Clean-up.
   */
   /* Close the port up. */
	mem_free(receiverslist);
		mem_free(messagebody);
   /* If a clean send, then reset and leave. */
//   ip_nonblock = iOld_ip_nonblock;
   return true;
}
