exchangelib icon indicating copy to clipboard operation
exchangelib copied to clipboard

Email with attachments sending bug

Open mike-egg123 opened this issue 3 years ago • 4 comments

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

mike-egg123 avatar Aug 04 '22 01:08 mike-egg123

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.

mike-egg123 avatar Aug 04 '22 02:08 mike-egg123

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).

ecederstrand avatar Aug 04 '22 07:08 ecederstrand

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

mike-egg123 avatar Aug 05 '22 01:08 mike-egg123

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?

ecederstrand avatar Aug 05 '22 05:08 ecederstrand

@mike-egg123 Did you ever find out what was going on in your end?

ecederstrand avatar Aug 16 '22 08:08 ecederstrand

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.

mike-egg123 avatar Aug 16 '22 12:08 mike-egg123

I'm closing the issue, but feel free to reopen if you get the chance to debug this further :-)

ecederstrand avatar Aug 29 '22 07:08 ecederstrand

OK. And I am afraid that I might not have a chance to debug. I quit the job, sorry.

mike-egg123 avatar Aug 29 '22 11:08 mike-egg123