#include <string.au3>
#include <Array.au3>
#Include <File.au3>
#Include <FF.au3>
#include <Date.au3>

Global $Paused
HotKeySet("{PAUSE}", "TogglePause")
HotKeySet("{ESC}", "Terminate")

$emailList="emaillist.csv"
$fowardEmailto="myemail@core.piehosting.com"
$cpanelEmailForwardingPage="https://yourcpanel.com:2082/cpsess8672753610/frontend/x3/mail/addfwd.html"

_FFConnect()

if _FFIsConnected() Then
	; do nothing
Else
	
	ConsoleWrite("Not Connected " & @CRLF)
EndIf

; check file if it exist
$file = FileOpen($emailList,0) 

if $file=-1 Then
	MsgBox(0,"error","File does not exist or cant be read")
	Exit
EndIf

; open file containing emails and put contents to an array
Dim $aRecords

_FileReadToArray($emailList,$aRecords)

MsgBox(0, "", "size: " & $aRecords[0])

For $x=1 to $aRecords[0]
	_FFOpenURL($cpanelEmailForwardingPage)
   If($aRecords[$x] <> $fowardEmailto) Then
	  writeForwarder($aRecords[$x])
   
	EndIf
Next

Exit

Func writeForwarder($email)
	$temp1 = StringSplit($email, "@")
	ConsoleWrite("email: " & $email & @CRLF)
	ConsoleWrite("name: " & $temp1[1] & @CRLF)
	ConsoleWrite("domain: " & $temp1[2] & @CRLF)
	
	; Enter email on field
	_FFSetValue($temp1[1], "email", "id")
	
	; Enter email domain on select dropdown box
	$test2='//select[@name="domain"]//option[@value="' & $temp1[2] & '"]'
    _FFXPath($test2, 'selected=true', 9)
	
	; Enter emaill address to forward email
	_FFSetValue($fowardEmailto, "fwdemail", "id")
	
	; submit fom
	_FFFormSubmit()
	
	;Sleep(2000)
EndFunc

Func TogglePause()
	$Paused = NOT $Paused
	While $Paused
		Sleep(100)
		ToolTip('Script is "Paused"',0,0)
		WEnd
	EndFunc
	
Func Terminate()
	Exit 0
	EndFunc