Jump to content
UBot Underground

Recommended Posts

Recently, I had to implement email related commands into one of my bots, to check a Gmail account for certain emails and after that delete the emails.

 

I started with POP3 settings, which worked great and fast, but I wasn't able to delete the emails, only to read them.

 

TIP:

After finding and using another solution (IMAP) to achieve the said results, a fellow UBot Forum member (entroqy) hinted at a Gmail account setting that might have helped, if I were aware of it prior to my attempt, that is making sure I allow POP3 to delete emails in my GMAIL Settings Page.

 

Some other webmail services may have 'delete emails' set as default, but gmail has 'keep emails on server' as their default setup there.

 

However, this made me want to look more into the two protocols that webmail services usually allow users to use to remotely access their accounts and that are also implemented in UBot:

 

POP3 vs. IMAP4

 

POP (stands for Post Office Protocol) - is like a one-way ticket.  It delivers your email from the server at once, in a one-time 'pop', in 3 steps:

  1. It connects to the server (through an Internet connection),
  2. downloads a copy of your awaiting messages via POP, and
  3. then disconnects.

There is no synchronization between your local copy and your email server.  Furthermore, POP is prone to losing messages or downloading the same messages multiple times.

 

IMAP (stands for Internet Message Access Protocol) - is the true two-way email management solution.  Any change made in any IMAP client (this includes UBot implementation) is synchronized with the remote email server.  Unlike POP, IMAP avoids loss of data through the two-way syncing process.

 

---------

 

If you're trying to decide between using POP3 vs. IMAP4, based on my experience, I would say that:

  • It is best to use POP when you only want to quickly check emails to see if say.. anything new was received, etc... as overall, I found POP to offer a much better speed which is sensibly obvious when you check large email accounts with many emails stored there already.
  • It is best to use IMAP when you want to grab the emails and store them on a local database/file of sorts and at the same time delete them from the remote server, to keep the email load there low and improve the speed of the connection every time you re-connect (the fewer emails the faster to get them and sync)

---------

 

Based on info shared freely on this forum and other places, especially lowridertj-botguru's contribution on this thread, I tried to assemble a file with connection settings for various webmail services that I will attach to this post.

 

Mind you, I am using only Gmail myself, so the other webmail settings are not tested by me, but feel free to PM me and I will update the file if you find anything that doesn't work as expected.

 

Email-Connections-Params.csv

 

I also have a small DEFINE script that basically loads the necessary connection data from that file, so that you don't need to ever remember all the needed settings but just select the proper webmail/protocol combo instead.

 

This script reads the above file and builds a table with the needed connection parameters. 

Inside the script there is another table built from yet another external file, containing your various accounts login details. 

For obvious reasons, that file is specific to each user, so I'm including a sample only (you will need to add your own logins inside)

 

Email-Accounts-Logins.csv

 

Based on the input variables (protocol and Email) the DEFINE selects the necessary data from the two tables and connects to the remote server, bringing the emails locally and adding them to a table (that you can choose to save as an external file later < add your own code here!) while at the same time attempting to delete them from the remote server IF that is at all possible.

 

loop(1) {
    set(#var_ENV_InboxTable_File, $ConnectWebMail("IMAP With SSL", "SteveOnGAEs@Gmail.com"), "Global")
}
divider
define $ConnectWebMail(#var_INP_EConn_Protocol, #var_INP_EConn_Account) {
    divider
    set(#var_INP_EConn_Protocol, $trim(#var_INP_EConn_Protocol), "Global")
    set(#var_LOC_EConn_Protocol, $change text casing(#var_INP_EConn_Protocol, "Lower Case"), "Global")
    divider
    set(#var_INP_EConn_Account, $trim(#var_INP_EConn_Protocol), "Global")
    set(#var_LOC_EConn_Account, $change text casing(#var_INP_EConn_Account, "Lower Case"), "Global")
    divider
    set(#var_LOC_Email_Acc_Web_Mail, $replace regular expression(#var_LOC_Email_Acc_Web_Mail, "(.+@)", $nothing), "Global")
    divider
    clear table(&tbl_ENV_Email_Connections)
    clear table(&tbl_ENV_EmailAccounts)
    clear table(&tbl_Inbox_Emails)
    divider
    if($comparison(#var_ENV_Email_AccLists_CHK, "=", 1)) {
        then {
            comment("Email Accounts Connect Details Already Loaded")
        }
        else {
            clear table(&tbl_ENV_Email_Connections)
            create table from file("D:\\My Stuff\\UBots Bots\\UBot V4 Bots\\Accounts\\Email-Connections-Params.csv", &tbl_ENV_Email_Connections)
            clear table(&tbl_ENV_EmailAccounts)
            create table from file("D:\\My Stuff\\UBots Bots\\UBot V4 Bots\\Accounts\\Email-Accounts-Logins.csv", &tbl_ENV_EmailAccounts)
            set(#var_ENV_Email_AccLists_CHK, 1, "Global")
        }
    }
    divider
    set(#var_CYC_Email_Con_CRT, $table total rows(&tbl_ENV_Email_Connections), "Global")
    loop while($comparison(#var_CYC_Email_Con_CRT, ">", 0)) {
        decrement(#var_CYC_Email_Con_CRT)
        divider
        set(#var_CYC_Email_Acc_ConnType, $table cell(&tbl_ENV_Email_Connections, #var_CYC_Email_Con_CRT, 1), "Global")
        set(#var_LOC_Email_Acc_ConnType, $trim(#var_CYC_Email_Acc_ConnType), "Global")
        set(#var_LOC_Email_Acc_ConnType, $change text casing(#var_LOC_Email_Acc_ConnType, "Lower Case"), "Global")
        divider
        if($comparison(#var_LOC_Email_Acc_ConnType, "=", #var_LOC_EConn_Protocol)) {
            then {
                set(#var_CYC_Email_Acc_EmServer, $table cell(&tbl_ENV_Email_Connections, #var_CYC_Email_Con_CRT, 2), "Global")
                set(#var_CYC_Email_Acc_Rem_Port, $table cell(&tbl_ENV_Email_Connections, #var_CYC_Email_Con_CRT, 3), "Global")
                divider
                set(#var_CYC_Email_Acc_CRT, $table total rows(&tbl_ENV_EmailAccounts), "Global")
                loop while($comparison(#var_CYC_Email_Acc_CRT, ">", 0)) {
                    decrement(#var_CYC_Email_Acc_CRT)
                    divider
                    set(#var_CYC_Email_Acc_EAddress, $table cell(&tbl_ENV_EmailAccounts, #var_CYC_Email_Acc_CRT, 0), "Global")
                    set(#var_LOC_Email_Acc_EAddress, $trim(#var_CYC_Email_Acc_EAddress), "Global")
                    set(#var_LOC_Email_Acc_EAddress, $change text casing(#var_LOC_Email_Acc_EAddress, "Lower Case"), "Global")
                    divider
                    set(#var_CYC_Email_Acc_Password, $table cell(&tbl_ENV_EmailAccounts, #var_CYC_Email_Acc_CRT, 1), "Global")
                }
            }
        }
    }
    divider
    connect to mail server(#var_INP_EConn_Protocol, #var_LOC_Email_Acc_EAddress, #var_CYC_Email_Acc_Password, #var_CYC_Email_Acc_EmServer, #var_CYC_Email_Acc_Rem_Port) {
        clear table(&tbl_Inbox_Emails)
        create table from emails(&tbl_Inbox_Emails)
        set(#var_CYC_Email_CRT, $table total rows(&tbl_Inbox_Emails), "Global")
        loop while($comparison(#var_CYC_Email_CRT, ">", 0)) {
            decrement(#var_CYC_Email_CRT)
            delete email(#var_CYC_Email_CRT)
        }
    }
}

You will have to alter the location of these two external data files, according to where you want to store them on YOUR computer.

 

Enjoy!

 

P.S.

Here is also the .ubot file:

 

Connect-to-WebMail-Sample-Bot.ubot

  • Like 5
Link to post
Share on other sites
  • 1 month later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...