I have two accounts on outlook 'user1@example.com' and 'user2@example.com'. I have a number of drafts in the user1 draft folder and want to update each email to the user2 address before sending them so that user2 is the sender of the emails and appears in the from field for the mail items.
using exchangelib I managed to change the 'sender' and 'account' address from user1 to user2 (and even print(item.sender, item.account)
to verify the change) but the update does not reflect on the emails' from field on the outlook draft folder when it's done.
import getpass
from exchangelib import Configuration
from exchangelib import Credentials, Account
from exchangelib import FileAttachment, HTMLBody
from exchangelib.properties import DistinguishedFolderId
def authenticate():
"""
Authenticate into mail.example.com
"""
email = "user1@example.com"
passwd = getpass.getpass(prompt="Enter your password: ")
user_credentials = Credentials(email, passwd)
config = Configuration(server="mail.example.com",
credentials=user_credentials)
account = Account(primary_smtp_address=email, config=config,
credentials=user_credentials, autodiscover=False)
return account
def main():
"""
Change sender account to user2@example.com
"""
user_account = authenticate()
drafts = DistinguishedFolderId('drafts')
for item in user_account.drafts.all().order_by('subject'):
item.sender = 'user2@example.com'
item.account = 'user2@example.com'
user_account.drafts.save(update_fields=['sender', 'account'])
exit("Done")
if __name__ == "__main__":
main()