1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

[REQ] Gmail Script

Discussion in 'Other Scripting Languages' started by TheMonster786, Jan 19, 2014.

Tags:
  1. TheMonster786

    TheMonster786 BANNED BANNED

    Joined:
    Dec 14, 2013
    Messages:
    160
    Likes Received:
    36
    Hi ,

    I need a Gmail Script that will automatically Move mails from Trash to Inbox . The script should be editable so that i can define the amount of mails move every time interval like i will be able to move 20 mails from Trash to Inbox Every 5 Minutes .

    If any1 would help me with this script , i would be highly Glad to hi .

    Thanks in advance .
     
  2. fxphil

    fxphil Senior Member

    Joined:
    Jul 16, 2010
    Messages:
    1,084
    Likes Received:
    504
    Out of curiosity ( I assume your working on a method of some sort) why have the e-mails hit the trash at all?

    DO they need to be direct on gmail?

    Without the information I could recommend setting up thunderbird then bulk moving e-mails.
     
  3. mypmmail

    mypmmail Junior Member

    Joined:
    Jan 31, 2008
    Messages:
    111
    Likes Received:
    27
    I did not really test the following code, thus, I would expect some degree of modification is required

    Code:
    $trash  = imap_open( '{imap.gmail.com:993/imap/ssl}Trash', $user, $pass );
    $emails = imap_search( $trash, 'ALL' );
    
    if( $emails ) {
        $count = 0;
        foreach( $emails as $email_uid ) {
            if ($count < 20) {
                imap_mail_move($trash, $email_uid, '{imap.gmail.com:993/imap/ssl}INBOX');
                $count = $count + 1;
            }
        }
    }
    
    On the other note, I think you can create filter in gmail itself to move the mail to some label so you can just concentrate on processing the mail.
     
    • Thanks Thanks x 1
  4. TheMonster786

    TheMonster786 BANNED BANNED

    Joined:
    Dec 14, 2013
    Messages:
    160
    Likes Received:
    36
    I don't now whether it will work or not but i must give a try . thanks for Helping .
     
  5. TheMonster786

    TheMonster786 BANNED BANNED

    Joined:
    Dec 14, 2013
    Messages:
    160
    Likes Received:
    36
    Why i Need this ?

    Ok , i am working with Dating CPA Offers . I have to use Autoresponder that automatically pops emails that comes to Inbox . I set that the poped mails will go in Trash . But you can say dnt Let them go to trash . Thats not the factor .

    I use the mails second time to another offer to increase my Revenue . So i have to manually move the Trash mails to Inbox . Its Really painful .

    Why i don't move all Trash Mails to Inbox ???
    >>> Coz the autoresponder will pop all or a huge amount of mails that will block my smtp .

    So to remain SAFE i badly Need the Script ....
     
  6. TheMonster786

    TheMonster786 BANNED BANNED

    Joined:
    Dec 14, 2013
    Messages:
    160
    Likes Received:
    36
    I found the Code Myself but need Small modification . It is moving 1 mail to Inbox Automatically every minute . But i want to increase the amount . Can't find out how to increase the amount !!!
    Here is the Code :


    function movetoinbox() {
    var firstThread = GmailApp.getTrashThreads(0,1)[0];
    firstThread.moveToInbox();

    }


    Can any1 please Tell me how to Increase it ???
     
    Last edited: Jan 20, 2014
  7. mypmmail

    mypmmail Junior Member

    Joined:
    Jan 31, 2008
    Messages:
    111
    Likes Received:
    27
    According to the documentation, it should goes something like
    Code:
        var twentythreads = GmailApp.getTrashThreads(0,20);
    
        for (index = 0; index < twentythreads .length; ++index) {
             twentythreads[index].moveToInbox();
        }
    
    However, I wish to point out that the script should be javascript.
    Where are you running this script?

    It's a bit confusing to me that you mentioned that the smtp server put your mail into your trash folder and you wish to move it to inbox.

    If you use the smtp server to send the mail, then but moving it to inbox won't help you send out the email.
    If the target mailbox has move the mail to trash, then you can't use the script to do it, because that's at the user's end (your prospect), so you can't really execute on their behalf either.

    It will be better to specify where do you want to this.
    Coz, running a php script and running a javascript is for every different purpose.
     
    • Thanks Thanks x 1
  8. TheMonster786

    TheMonster786 BANNED BANNED

    Joined:
    Dec 14, 2013
    Messages:
    160
    Likes Received:
    36

    Thanks BTW i tried this earlier , No result !!! t moves ONLY 1 Mail as well .

    How i use it ?

    Login to a Gmail Account& then open script.google.com in a new tab . You can create script for Gmail there as well as other Google services .

    So make a new Script & put the code in the Script . After that run the Script . You can see the result in your gmail .

    For example , if u use this Script , If you have mails in trash , every time u run this script , one mail will move to Inbox .

    Hope you Understand .

    waiting for another Solution . . .
     
  9. TheMonster786

    TheMonster786 BANNED BANNED

    Joined:
    Dec 14, 2013
    Messages:
    160
    Likes Received:
    36
    Hurray .... Working ... Just a Small modification was Needed !!!!

    function movetoInbox() {

    var Inbox = GmailApp.getTrashThreads(0,20);

    for (i = 0; i< Inbox.length; i++) {
    Inbox .moveToInbox();
    }

    }

    Thanks Man ....
     
    • Thanks Thanks x 1
  10. mypmmail

    mypmmail Junior Member

    Joined:
    Jan 31, 2008
    Messages:
    111
    Likes Received:
    27
    I have tried this code, work for me with 2 threads.
    i.e.
    I have 3 threads in the trash, and ran the code.
    After that, left 1 thread (which is correct)

    Code:
    function processInbox() {
      // get all threads in inbox
      var threads = GmailApp.getTrashThreads(0, 2);
      for (var i = 0; i < threads.length; i++) {
        threads[i].moveToInbox();
      }
    

    Added:
    Sorry, only saw your updated post after I posted
     
  11. TheMonster786

    TheMonster786 BANNED BANNED

    Joined:
    Dec 14, 2013
    Messages:
    160
    Likes Received:
    36

    Thanks .

    I am willing to Make another Script That will Automatically Send Email from Gmail's Inbox to the Sender as REPLY .



    Cnt figure out how to start ? Waiting for some Suggestions .,...
     
    Last edited: Jan 21, 2014
  12. mypmmail

    mypmmail Junior Member

    Joined:
    Jan 31, 2008
    Messages:
    111
    Likes Received:
    27

    I would probably try with something like this, but, disclaimer on my end, is that I have not tested this out as I don't want to run this code on my inbox with actual email.

    Code:
    function processInbox() {
      // get only 1 thread in inbox, change 1 to any number you wish
      var threads = GmailApp.getInboxThreads(0,1);
      for (var i = 0; i < threads.length; i++) {
        // get only the first message
        var message = threads[i].getMessages()[0];
        message.reply("Your reply message");
      }
    
    Based on example shown on hxxps://developers.google.com/apps-script/reference/gmail/gmail-message#reply(String)
     
    • Thanks Thanks x 1
  13. TheMonster786

    TheMonster786 BANNED BANNED

    Joined:
    Dec 14, 2013
    Messages:
    160
    Likes Received:
    36
    It works but not the way i want . It is Replying only the first email every time it process .

    i want it to be like this :

    It will Reply 1 or more Incoming mail every time it runs ( You done the coding ) & that Replied email will Go to Trash ( Need Code for this )

    Code:
    
    function processInbox() {
      // get only 1 thread in inbox, change 1 to any number you wish
      var threads = GmailApp.getInboxThreads(0,1);
      for (var i = 0; i < threads.length; i++) {
        // get only the first message
        var message = threads[i].getMessages()[0];
        message.reply(" hi ,Thanks For Your Reply . ");
        threads [i].moveToTrash();
      }
    }
    
    

    Moving to Trash Is Done .


    It would be Better if it could Fetch The First name in My Reply . Like if xyz@gmail.com has a first name named aaa , I want to put his First name in my Reply . ( Need Code for this )

    message.reply(" Hi %FNAME% , Your reply message");

    It Should Also Fetch the Body of The Incoming Email in Reply .
     
    Last edited: Jan 22, 2014
  14. mypmmail

    mypmmail Junior Member

    Joined:
    Jan 31, 2008
    Messages:
    111
    Likes Received:
    27
    Ok, you probably have to refer to this method call instead, which will give you much more options
    hxxps://developers.google.com/apps-script/reference/gmail/gmail-message#reply(String,Object)

    Basically, you need to construct a options JSON object and then supply it as input to the reply method and you are good to go.
    As for how to retrieve the name of sender and message body, you can use the api getFrom() and getPlainBody()

    So, the code can be further added like this
    Code:
    function processInbox() {
      // get only 1 thread in inbox, change 1 to any number you wish
      var threads = GmailApp.getInboxThreads(0,1);
      for (var i = 0; i < threads.length; i++) {
        // get only the first message
        var message = threads[i].getMessages()[0];
    
        var name = message.getFrom(); // you will likely need to further split the name
        var body = message.getPlainBody();
    
        var content = "Hi " + name + ",Thanks For Your Reply." + body;
    
        // refer to the documentation on what are the options you can set
        var options = {
    	"name":"Your Name to Be Shown - maybe company name instead of your name",
    	"subject":"a new subject", 
    	"htmlBody":content
        };
    
        message.reply(content, options);
    
        threads [i].moveToTrash();
      }
    }
    
    
     
    • Thanks Thanks x 1
  15. TheMonster786

    TheMonster786 BANNED BANNED

    Joined:
    Dec 14, 2013
    Messages:
    160
    Likes Received:
    36
    I am on the way , will check it later . but I don't think it will work as I want . Thanks dude . you are very helpful .
     
  16. TheMonster786

    TheMonster786 BANNED BANNED

    Joined:
    Dec 14, 2013
    Messages:
    160
    Likes Received:
    36


    Nice Work . Need some Small Modifications.


    Code:
    var content = "Hi " + name + ",Thanks For Your Reply." + body;
    
    if i use this code , the reply comes to only one line . Like Hi mary , Thanks FOr your Reply . Body of sender's mail .

    I want a line Break . i want it to be like this .

    Code:
    Hi mary ,
    
    Thanks for Your Reply .
    
    
    Body 
    
    
    i also want to put html Link in My Reply . Like <a href="" > Bang </a>

    is it possible ????
     
    Last edited: Jan 22, 2014
  17. mypmmail

    mypmmail Junior Member

    Joined:
    Jan 31, 2008
    Messages:
    111
    Likes Received:
    27
    Yes, it's possible.

    If you notice, the "content" is specified in both the options and the message.reply().

    They should actually be slightly different.
    Because the options is specifying the htmlBody which is html content, while the message.reply is specifying the plain text content.

    So, technically, it should be something like setting the plain text for one and html for another.

    The mail client will determine whether it can support html and use the correct content accordingly.

    Code:
    function processInbox() {
      // get only 1 thread in inbox, change 1 to any number you wish
      var threads = GmailApp.getInboxThreads(0,1);
      for (var i = 0; i < threads.length; i++) {
        // get only the first message
        var message = threads[i].getMessages()[0];
    
        var name = message.getFrom(); // you will likely need to further split the name
        var body = message.getPlainBody();
    
        // plain text use \n for new line
        var textcontent = "Hi " + name + ",Thanks For Your Reply.\n" + body;
    
        var htmlcontent = "Hi " + name + ",Thanks For Your Reply.<br/><a href=''>Click</a><br/>" + body; 
    
        // refer to the documentation on what are the options you can set
        var options = {
    	"name":"Your Name to Be Shown - maybe company name instead of your name",
    	"subject":"a new subject", 
    	"htmlBody":htmlcontent 
        };
    
        message.reply(textcontent, options);
    
        threads [i].moveToTrash();
      }
    }
    
    
     
    • Thanks Thanks x 1
  18. mypmmail

    mypmmail Junior Member

    Joined:
    Jan 31, 2008
    Messages:
    111
    Likes Received:
    27
    Btw, just a side comment on another topic where people ask how to learn programming.

    I think this is a good example of starting with a small idea.
    i.e. send email.
    From there, you will inevitably need more function and as you discover the need for more functions, you search more ways to do it and you improve your knowledge from there.
    Regardless of how much you use this after that, you definitely learn something. Or, at least, I hope. :)
     
  19. TheMonster786

    TheMonster786 BANNED BANNED

    Joined:
    Dec 14, 2013
    Messages:
    160
    Likes Received:
    36


    MindBlowing .


    Code:
    
     var textcontent = "Hi " + name + ",Thanks For Your Reply.\n" + body;
    
    

    Its fetching the whole name ; both FIRST name & LAST name BTW i want to Fetch only FIRST NAME .

    How Can i ?

    & Regarding your Programming Learning , Can you please tell me Where can i learn JAVA Easily & Effectively ?

    Thanks
     
  20. TheMonster786

    TheMonster786 BANNED BANNED

    Joined:
    Dec 14, 2013
    Messages:
    160
    Likes Received:
    36
    This is what Exactly i want . please take a look & Gimme a Solution .

    Screenshot_35.jpg


    Screenshot_36.jpg

    Screenshot_37.jpg