% *** This function is part of ERPLAB Toolbox ***
% Author: Javier Lopez-Calderon
% Davis, CA
% April 2012
%

function erpcom = pop_senderpbymail(ALLERP, erpindex, mailto, subject, message, attach)
erpcom = '';
if nargin==1
        try
                if isempty(getpref('Internet','SMTP_Username')) || isempty(getpref('Internet','SMTP_Password'))
                        msgboxText = ['You have not set neither your gmail nor your yahoo email account to use this tool.\n\n'...
                                'Please, go to Utilities menu, Set Gmail or Yahoo email account'];
                        title = 'ERPLAB: pop_senderpbymail requieres an email account';
                        errorfound(sprintf(msgboxText), title);
                        return
                end
        catch
                addpref('Internet','SMTP_Username', [])
                addpref('Internet','SMTP_Password', [])
                msgboxText = ['You have not set neither your gmail nor your yahoo email account to use this tool.\n\n'...
                        'Please, go to Utilities menu, Set Gmail or Yahoo email account'];
                title = 'ERPLAB: pop_senderpbymail requieres an email account';
                errorfound(sprintf(msgboxText), title);
                return
        end
        if isempty(ALLERP)
                msgboxText =  'No ERPset was found!';
                title_msg  = 'ERPLAB: pop_senderpbymail() error:';
                errorfound(msgboxText, title_msg);
                return
        end
        
        def = erpworkingmemory('sendemailGUI');
        if isempty(def)
                def = {'','You got an ERPmail!', 1};
        end
        
        %
        % Call GUI
        %
        answer  = sendemailGUI(def);
        
        if isempty(answer)
                disp('User selected Cancel')
                return
        end
        
        mailto   = answer{1};
        subject  = answer{2};
        msg      = answer{3};
        erpindex = answer{4}; % numeric  
        
        erpworkingmemory('sendemailGUI', {mailto, subject, erpindex});        
        erpcom   = pop_senderpbymail(ALLERP, erpindex, mailto, subject, msg);
        return
else
        if isempty(getpref('Internet','SMTP_Username')) || isempty(getpref('Internet','SMTP_Password'))
                error('prog:input','ERPLAB says: You have not set neither your gmail nor your yahoo email account to use this tool.\n\nPlease, go to Utilities menu, Set Gmail or Yahoo email account');
        end
        if nargin<6
                attach = [];
        end
        if nargin<5
                message = {sprintf('...sent from ERPLAB.\n\n')};
        else
                message = [message; sprintf('\n\nSent from ERPLAB.\n\n')];
        end
        if nargin<4
                subject = '...';
        end
end
if ~isempty(erpindex)
        p = which('eegplugin_erplab', '-all');
        if length(p)>1
                fprintf('\nERPLAB WARNING: More than one ERPLAB folder was found.\n\n');
        end
        p = p{1};
        p = p(1:findstr(p,'eegplugin_erplab.m')-1);        
        pathname = fullfile(p,'erplab_Temp'); % path for temporary storage        
        erpindex = unique(erpindex);
        nerpset = length(erpindex);
        
        for i=1:nerpset
                erpname  = ALLERP(erpindex(i)).erpname;
                filename = strtrim(ALLERP(erpindex(i)).filename);
                if isempty(filename)
                        filename = [erpname '_atmenu_' num2str(erpindex(i))];
                end
                pop_savemyerp(ALLERP(erpindex(i)), 'erpname', erpname, 'filename', [filename '.erp'], 'filepath', pathname, 'warning', 'off');
                pause(0.1)
                attach{i} = fullfile(pathname,[filename '.zip']);
                fprintf('Zipping %s....\n', [filename '.zip']);
                zip(attach{i}, [filename '.erp']);
                pause(0.1)
        end
end

fprintf('Sending e-mail....\n\n');
try
        sendmail(mailto,subject,message,attach)
        fprintf('Your e-mail was sent.\n');
catch
        msgboxText = ['Oops! ERPLAB could not make your mail account to work.\n'...
                'So, you may try gain.\n\n'...
                'HINT: If this problem persists, try moving to another internet connection.'];
        title = 'ERPLAB: pop_senderpbymail not working';
        errorfound(sprintf(msgboxText), title);
        return
end

%
% Completion statement
%
msg2end

if iscell(mailto)
        mailtostr = char(mailto{1});
end
if iscell(attach)
        attachstr = char(attach{1});
end
if isempty(attach)
        erpcom = sprintf('pop_senderpbymail(%s, %s, {''%s''}, ''%s'');', inputname(1), vect2colon(erpindex), mailtostr, subject);
else
        erpcom = sprintf('pop_senderpbymail(%s, %s, {''%s''}, ''%s'', {''%s''});', inputname(1), vect2colon(erpindex), mailtostr, subject, attachstr);
end




