Email with attachments sending bug
Describe the bug I attached the files in an email and send it. In the reciever, I could find the email and the attachment, but in the sent folder of the primary email, I couldn't find this email at all.
To Reproduce
creds = Credentials(
username='***',
password='***'
)
config = Configuration(server='***', credentials=creds)
account = Account(
primary_smtp_address='***',
credentials=creds,
autodiscover=False,
access_type=DELEGATE,
config=config
)
m = Message(
account=account,
folder=account.sent,
subject=subject,
body=HTMLBody(body),
to_recipients=[Mailbox(email_address=to_addr)]
)
attachments = []
for attachment in attachment_list:
with open(attachment, 'rb') as f:
content = f.read()
file_name = attachment.split("/")[-1]
attachments.append((file_name, content))
for attachment_name, attachment_content in attachments:
file = FileAttachment(name=attachment_name, content=attachment_content)
m.attach(file)
m.send_and_save()
Additional context Python version: 3.6.9 exchangelib version: 4.6.2
I figured out this problem, don't use send_and_save() but use:
m.save()
m.send()
Then I can watch the email with attachments in the sent folder of primary email.
Thanks for the report!
I cannot reproduce this using a minimal example which would be:
a = Account(...)
m = Message(folder=a.sent, subject="Foo", to_recipients=[a.primary_smtp_address])
m.attach(FileAttachment(name="Baz.txt", content=b"xyz"))
m.send_and_save()
This gives me one email in the sent folder, and eventually another in my inbox. Both have the attachment.
Which version of Exchange is this? You can check with print(account.version).
I am glad that you reply so quickly! And I am sorry to reply late. Here is the output of print(account.version):
Build=14.3.409.0, API=Exchange2010_SP2, Fullname=Microsoft Exchange Server 2010 SP2
Hmm, that's weird. This has come up before, and we should already be reverting to save+send for Exchange versions prior to 2013. See https://github.com/ecederstrand/exchangelib/blob/master/exchangelib/items/message.py#L108
Are you able to add a breakpoint in send_and_save() and debug why that's apparently not happening in your case?
@mike-egg123 Did you ever find out what was going on in your end?
Hello, I'm sorry to tell you that because of the epidemic control policy, I was forced to stay at home and couldn't go to work. The code was on the company's computer, so I couldn't help you check this bug during this period of time. Once I go to work, I will try to find out.
I'm closing the issue, but feel free to reopen if you get the chance to debug this further :-)
OK. And I am afraid that I might not have a chance to debug. I quit the job, sorry.