How to reply to the last email (IMAP4) in inbox?












2















Python IMAP4 how to reply to an email with a given UID or last email in Inbox



I have been able to login my gmail with IMAP4 and get the last UID , but I dont know how to reply to the last email in the inbox my python automation code:



    self.mail = imaplib.IMAP4_SSL('imap.gmail.com')
self.mail.login(self.data["emailUsername"], self.data["emailPassword"])
self.mail.list()
self.mail.select("inbox")
subjectStr = '(HEADER Subject ' + """ + mySubject + """ + ')'
result, UIDemailsWithGivenSubject = self.mail.uid('search', None, subjectStr)
ids_string = UIDemailsWithGivenSubject[0].decode("utf-8")
ids_string_list = ids_string.split(" ")
self.lastEmailUid = ids_string_list[-1]


Now how do I respond to the last email or respond/reply with a given UID.










share|improve this question





























    2















    Python IMAP4 how to reply to an email with a given UID or last email in Inbox



    I have been able to login my gmail with IMAP4 and get the last UID , but I dont know how to reply to the last email in the inbox my python automation code:



        self.mail = imaplib.IMAP4_SSL('imap.gmail.com')
    self.mail.login(self.data["emailUsername"], self.data["emailPassword"])
    self.mail.list()
    self.mail.select("inbox")
    subjectStr = '(HEADER Subject ' + """ + mySubject + """ + ')'
    result, UIDemailsWithGivenSubject = self.mail.uid('search', None, subjectStr)
    ids_string = UIDemailsWithGivenSubject[0].decode("utf-8")
    ids_string_list = ids_string.split(" ")
    self.lastEmailUid = ids_string_list[-1]


    Now how do I respond to the last email or respond/reply with a given UID.










    share|improve this question



























      2












      2








      2








      Python IMAP4 how to reply to an email with a given UID or last email in Inbox



      I have been able to login my gmail with IMAP4 and get the last UID , but I dont know how to reply to the last email in the inbox my python automation code:



          self.mail = imaplib.IMAP4_SSL('imap.gmail.com')
      self.mail.login(self.data["emailUsername"], self.data["emailPassword"])
      self.mail.list()
      self.mail.select("inbox")
      subjectStr = '(HEADER Subject ' + """ + mySubject + """ + ')'
      result, UIDemailsWithGivenSubject = self.mail.uid('search', None, subjectStr)
      ids_string = UIDemailsWithGivenSubject[0].decode("utf-8")
      ids_string_list = ids_string.split(" ")
      self.lastEmailUid = ids_string_list[-1]


      Now how do I respond to the last email or respond/reply with a given UID.










      share|improve this question
















      Python IMAP4 how to reply to an email with a given UID or last email in Inbox



      I have been able to login my gmail with IMAP4 and get the last UID , but I dont know how to reply to the last email in the inbox my python automation code:



          self.mail = imaplib.IMAP4_SSL('imap.gmail.com')
      self.mail.login(self.data["emailUsername"], self.data["emailPassword"])
      self.mail.list()
      self.mail.select("inbox")
      subjectStr = '(HEADER Subject ' + """ + mySubject + """ + ')'
      result, UIDemailsWithGivenSubject = self.mail.uid('search', None, subjectStr)
      ids_string = UIDemailsWithGivenSubject[0].decode("utf-8")
      ids_string_list = ids_string.split(" ")
      self.lastEmailUid = ids_string_list[-1]


      Now how do I respond to the last email or respond/reply with a given UID.







      python email imap






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 22:06









      martineau

      69.9k1092186




      69.9k1092186










      asked Jan 2 at 21:20









      user3556956user3556956

      1308




      1308
























          1 Answer
          1






          active

          oldest

          votes


















          1














          You can search the selected mailbox like this:



          res, data = self.mail.search(None, 'ALL')


          Your data will then look something like this: [b'1 2 3 4 5 6 7 8 9 10 11 12'] where 12 is the UID for the latest email in the selected mailbox. You can now fetch this mail with



          self.mail.fetch(data[0].split()[-1], '(RFC822)')


          Explanation



          IMAP4.search(charset, criterion[, ...]) returns a list of all emails in your current mailbox. They are typically orderd by date with the first one being the oldest.
          Provide None for the charset to search for any mail and the criterion is set to All.



          See Python imaplib documentation



          data[0].split()[-1] returns the last element ([-1]) of your list of mail UIDs



          Respond to mail



          You are using the IMAP protocol to access a mailserver. IMAP stands for




          Internet Message Access Protocol




          It is intendet for message access, not to send mail. You need SMTP to send messages. Take a look at this question.






          share|improve this answer


























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54013333%2fhow-to-reply-to-the-last-email-imap4-in-inbox%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            You can search the selected mailbox like this:



            res, data = self.mail.search(None, 'ALL')


            Your data will then look something like this: [b'1 2 3 4 5 6 7 8 9 10 11 12'] where 12 is the UID for the latest email in the selected mailbox. You can now fetch this mail with



            self.mail.fetch(data[0].split()[-1], '(RFC822)')


            Explanation



            IMAP4.search(charset, criterion[, ...]) returns a list of all emails in your current mailbox. They are typically orderd by date with the first one being the oldest.
            Provide None for the charset to search for any mail and the criterion is set to All.



            See Python imaplib documentation



            data[0].split()[-1] returns the last element ([-1]) of your list of mail UIDs



            Respond to mail



            You are using the IMAP protocol to access a mailserver. IMAP stands for




            Internet Message Access Protocol




            It is intendet for message access, not to send mail. You need SMTP to send messages. Take a look at this question.






            share|improve this answer






























              1














              You can search the selected mailbox like this:



              res, data = self.mail.search(None, 'ALL')


              Your data will then look something like this: [b'1 2 3 4 5 6 7 8 9 10 11 12'] where 12 is the UID for the latest email in the selected mailbox. You can now fetch this mail with



              self.mail.fetch(data[0].split()[-1], '(RFC822)')


              Explanation



              IMAP4.search(charset, criterion[, ...]) returns a list of all emails in your current mailbox. They are typically orderd by date with the first one being the oldest.
              Provide None for the charset to search for any mail and the criterion is set to All.



              See Python imaplib documentation



              data[0].split()[-1] returns the last element ([-1]) of your list of mail UIDs



              Respond to mail



              You are using the IMAP protocol to access a mailserver. IMAP stands for




              Internet Message Access Protocol




              It is intendet for message access, not to send mail. You need SMTP to send messages. Take a look at this question.






              share|improve this answer




























                1












                1








                1







                You can search the selected mailbox like this:



                res, data = self.mail.search(None, 'ALL')


                Your data will then look something like this: [b'1 2 3 4 5 6 7 8 9 10 11 12'] where 12 is the UID for the latest email in the selected mailbox. You can now fetch this mail with



                self.mail.fetch(data[0].split()[-1], '(RFC822)')


                Explanation



                IMAP4.search(charset, criterion[, ...]) returns a list of all emails in your current mailbox. They are typically orderd by date with the first one being the oldest.
                Provide None for the charset to search for any mail and the criterion is set to All.



                See Python imaplib documentation



                data[0].split()[-1] returns the last element ([-1]) of your list of mail UIDs



                Respond to mail



                You are using the IMAP protocol to access a mailserver. IMAP stands for




                Internet Message Access Protocol




                It is intendet for message access, not to send mail. You need SMTP to send messages. Take a look at this question.






                share|improve this answer















                You can search the selected mailbox like this:



                res, data = self.mail.search(None, 'ALL')


                Your data will then look something like this: [b'1 2 3 4 5 6 7 8 9 10 11 12'] where 12 is the UID for the latest email in the selected mailbox. You can now fetch this mail with



                self.mail.fetch(data[0].split()[-1], '(RFC822)')


                Explanation



                IMAP4.search(charset, criterion[, ...]) returns a list of all emails in your current mailbox. They are typically orderd by date with the first one being the oldest.
                Provide None for the charset to search for any mail and the criterion is set to All.



                See Python imaplib documentation



                data[0].split()[-1] returns the last element ([-1]) of your list of mail UIDs



                Respond to mail



                You are using the IMAP protocol to access a mailserver. IMAP stands for




                Internet Message Access Protocol




                It is intendet for message access, not to send mail. You need SMTP to send messages. Take a look at this question.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jan 2 at 22:13

























                answered Jan 2 at 22:03









                Jonas DrotleffJonas Drotleff

                346215




                346215
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54013333%2fhow-to-reply-to-the-last-email-imap4-in-inbox%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    MongoDB - Not Authorized To Execute Command

                    in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                    Npm cannot find a required file even through it is in the searched directory