I want to reply to gmail via imaplib.

Asked 2 years ago, Updated 2 years ago, 64 views

Purpose
I would like to reply to the email that arrived at my email address.
You will automatically remind yourself of an email that arrived to you.

code

account="[email protected]"
password="hogehoge"

s_title=str("Mail Test")

mail=imaplib.IMAP4_SSL ('imap.gmail.com',993)
mail.login(account, password)
mail.select('inbox')

term=s_title.encode ("utf-8")
print(term)
mail.literal=term
type, data=mail.search("utf-8", "SUBJECT")

for i in data [0].split():
    ok, x = mail.fetch(i, 'RFC822')
    ms = email.message_from_string(x[0][1].decode('UTF-8'))

    ad=email.header.decode_header(ms.get('From')))
    ms_code=ad[0][1]

    fromObj=email.header.decode_header(ms.get('From')))
    addr=""
    
    for in fromObj:
        if isinstance(f[0], bytes):
            addr+=f[0].decode(ms_code)
        else:
            addr+=f[0]
            print(addr)

    subject=email.header.decode_header(ms.get('Subject')))
    title=""

    for sub in subject:
        if isinstance(sub[0], bytes):
            title+=sub[0].decode(ms_code)
        else:
            title+=sub[0]
            print(title)

    if ms.is_multipart():
        for payload in ms.get_payload():
            if payload.get_content_type()=="text/plain":
                body=payload.get_payload()

            else:
                if ms.get_content_type() == "text/plain":
                    body=ms.get_payload()

new=MIMEMultipart()
new ["Message-ID"] = email.utils.make_msgid()
new ["In-Reply-To"] = account
new ["References" = original ["Message-ID" ]
new ["Subject"] = original ["Subject"]
new["To" = original["Reply-To"] original["From"]
new ["From"] = account

new.attach (MIMEMessage (original)

s= smtplib.SMTP ("smtp.gmail.com", 587)
s.starttls()
s.login(account, password)
s.send_message(new)
s.quit()

I have searched the email once and tried to reply with the title, but it was created as a new email instead of a reply.
I thought about loading thread IDs, but I used this method because the IDs might change.
I cannot use the API for personal reasons, so I am looking for a way other than API.)

I would appreciate it if you could let me know because I can't find much information about replying to your email.

python gmail

2022-09-30 16:12

1 Answers

I didn't actually try it, I just looked it up a little bit, but for example, the following article explains that Gmail replies require "threadId" and "RFC2822 format message ID".

Send email (reply) to Thread in the Gmail API Client for Python

Gmail API reference users.messages, the conditions for connecting mail to Thread are

The ID of the thread the message bells to.To add a message or draft to a thread, the following criteria must be met:

It says.Information you need here is

  • threadId
  • Message ID in RFC 2822 format

There are two.You must specify not only what mail to reply to in Thread, but also what mail to reply to in Thread.


2022-09-30 16:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.