Skip to content

Instantly share code, notes, and snippets.

@nsapa
Last active July 20, 2022 12:28
Show Gist options
  • Save nsapa/bf9f2c552a573be97e03de3f8e67ad23 to your computer and use it in GitHub Desktop.
Save nsapa/bf9f2c552a573be97e03de3f8e67ad23 to your computer and use it in GitHub Desktop.
Enable X-Mailer & X-Received on EWS email for a specific account
#!/usr/bin/env python
# python -m pip install -U exchangelib
from exchangelib import Credentials, Account, Configuration, ExtendedProperty, Message, Mailbox
# Microsoft doc: https://docs.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-provision-x-headers-by-using-ews-in-exchange
# ExchangeLib doc: https://ecederstrand.github.io/exchangelib/#extended-properties
class XReceived(ExtendedProperty):
property_name = 'X-Received'
property_type = 'String'
distinguished_property_set_id = 'InternetHeaders'
class XMailer(ExtendedProperty):
distinguished_property_set_id = 'InternetHeaders'
property_name = 'X-Mailer'
property_type = 'String'
credentials = Credentials(username='[your_domain]\\[your_user]', password='[redacted]')
config = Configuration(service_endpoint='https://[your_mail_server]/EWS/Exchange.asmx', credentials=credentials)
my_account = Account( primary_smtp_address='[your_email]', credentials=credentials, config=config)
message = Message()
message.register('xreceived',XReceived)
message.register('xmailer',XMailer)
message.to_recipients=[ Mailbox(email_address="[an_external_recipent]") ]
message.subject='Hello, World!'
message.body='This is a test'
message.xreceived='test'
message.xmailer='Toto!'
message.account = my_account
message.send()
@ecederstrand
Copy link

register() is a class method and should be called on Message, not message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment