SVill
SVill

Reputation: 439

Python Outlook Sent Folder

I created an automated email sender in Python for Outlook. It works fine, but I was wondering if it is possible to save the emails it sends in the sent folder. I'm sure there is, but I am unsure where to begin. Any help would be appreciated.

This is in 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()

Upvotes: 1

Views: 2304

Answers (1)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66316

Sending via SMTP will not copy the messages into the Sent Items folder. You will need to use Outlook Object Model (via win32com) or EWS (in case of Exchange Server).

UPDATE: as of Summer 2019, messages sent through Office 365 SMTP servers are saved in the Sent Items folder of the sending account mailbox.

Upvotes: 1

Related Questions