0
imaplib
을 사용하여 gmail받은 편지함을 검사하고 특정 제목과 일치하지 않는 전자 메일을 별도 전자 메일로 전달하는 프로그램을 작성해야합니다. stmplib
을 사용하여이 작업을 수행 할 수 있지만 전달해야하고 삭제해야하는 이메일에 첨부 파일이 포함되어있는 경우 이메일을 보내는 방법을 알지 못합니다. 다음은 첨부 파일이있는 전자 메일을 처리 할 수 있도록 업데이트해야하는 코드입니다.Python을 사용하여 첨부 파일이있는 전자 메일을 전달하는 방법
# Connect and login to email
imap = imaplib.IMAP4_SSL('imap.gmail.com')
imap.login('[email protected]','password')
imap.list()
imap.select('inbox')
smtp = smtplib.SMTP_SSL('smtp.gmail.com')
smtp.login('[email protected]','password')
try:
#Search and return sequential ids
result, data = imap.search(None,'ALL')
ids_list = data[0].split()
#print 'Total emails: '+str(len(ids_list))
latest_id = ids_list[-1]
#Process each email in inbox
for i in ids_list:
t, d = imap.fetch(i, '(RFC822)')
for res_part in d:
if isinstance(res_part, tuple):
text = res_part[1]
msg = email.message_from_string(text)
subject = msg['subject']
#print 'Subject: '+subject
message = get_txt(msg) #Retrieves email body text
#print message
if subject != 'The subject I\'m looking for': #Junk email
#print 'Sending to another email...'
smtp.sendmail('[email protected]', '[email protected]', message)
imap.store(i, '+FLAGS', '\\Deleted')
imap.expunge()
else: #Email we need to process
#print 'Process this email'
except IndexError:
#Inbox is empty
누군가 내게 이것을 달성하기위한 적절한 방법을 보여줄 수 있습니까? 감사!
@abccd 현재 가지고있는 코드로 업데이트했습니다. 현재 이메일 본문 만 처리하는 동안 이메일 첨부 파일을 처리 할 수 있어야합니다. – McFizz