I am trying to create and save message to Drafts in gmail, but nothing happen.
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()
When I am changing msg.as_bytes() to msg.as_string(), I got the error below:
TypeError: cannot use a bytes pattern on a string-like object