D
D
Dmitry2017-11-02 04:52:23
Python
Dmitry, 2017-11-02 04:52:23

How to save a letter in drafts?

There is this code:

import time
import random
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import imaplib

def save_draft(email_to, body, login, password, image):
    msg = MIMEMultipart("alternative")
    msg.set_charset("utf-8")
    msg.attach(MIMEText(body, "plain", "utf-8"))
    msg['Subject'] = SUBJECT
    msg['From'] = login
    msg['To'] = email_to
    with open(image, 'rb') as f:
        part = MIMEApplication(
            f.read(),
            Name=image
        )
    part['Content-Disposition'] = 'attachment; filename={}'.format(IMAGE)
    msg.attach(part)

    imap = imaplib.IMAP4_SSL("imap.gmail.com", 993)
    imap.login(login, password)
    imap.select('[Gmail]/Drafts')

    now = imaplib.Time2Internaldate(time.time())
    imap.append('[Gmail]/Drafts',
                '',
                now,
                msg.as_bytes())
    imap.logout()

It seems like it should create a new message and save it in gmail as a draft, but for some reason this does not happen.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question