2017-01-24 78 views
0

我在Outlook中爲Python創建了一個自動化的電子郵件發件人。它工作正常,但我想知道是否可以保存它發送到發送文件夾中的電子郵件。我確定有,但我不確定從哪裏開始。任何幫助,將不勝感激。Python Outlook發送文件夾

這是在Python 3.6

======

from tkinter import * 
import smtplib 
from email.mime.multipart import MIMEMultipart 
from email.mime.text import MIMEText 
from email.mime.image import MIMEImage 
import csv 
import time 
import warnings 


root = Tk() 
root.geometry('200x200') 

email_label = Label(root, text="Enter your email") 
email_label.pack() 

username = Entry(root, width = 30) 
username.pack() 

password_label = Label(root, text="Enter your password") 
password_label.pack() 

password = Entry(root, show="*", width = 30) 
password.pack() 

def add_var(): 
    user_name = username.get() 
    pass_word = password.get() 
    with open("emailtk.csv") as f: 
     try: 
      reader = csv.reader(f) 
      for row in reader: 
      time.sleep(3) 
       address = row[0] 
       first_name = row[1] 
       last_name = row[2] 
       name = first_name+' '+last_name 
       company = row[4] 
       msg = MIMEMultipart() 
       msg["To"] = address 
       msg["From"] = user_name 
       msg["Subject"] = subject 
       print("Will now send an email to %s at %s at %s" % (name, company, address)) 
       msgText = MIMEText(""" 
           Hello %s! 
           """ % (name), 'html') 
       msg.attach(msgText) # Added, and edited the previous line 

       time.sleep(5) 

       smtp = smtplib.SMTP('Outlook.com', 25) 
       smtp.ehlo() 
       smtp.starttls() 
       smtp.login(user_name,pass_word) 
       smtp.sendmail(user_name, address, msg.as_string()) 
       print("email sent") 
       print("======================") 
       print() 
       smtp.quit() 
+0

你是如何發送電子郵件?我的實現使用'win32com'將電子郵件按預期放入發送的文件夾中。 – Jkdc

+0

通過smtp。我將把代碼放在問題中。 – SVill

回答

0

通過SMTP發送不會將郵件複製到發送郵件文件夾。您將需要使用Outlook對象模型(通過win32com)或EWS(對於Exchange Server)。

+0

或使用IMAP將其放入已發送文件夾中。 – Max

+0

這不會是由Outlook發送的相同消息(MIME頭和全部) - 它將是由您的代碼創建的假消息。 –

+0

您認爲標準IMAP/SMTP客戶端如何做到這一點?他們將他們通過SMTP提交的相同消息放入IMAP中(儘管可能添加了BCC頭文件)。 – Max