Comments (10)
from exchangelib.
I didn't know myself, but this is actually possible with EWS using either the SendItem
service with existing draft items, or with the CreateItem
service. We don't support the former service yet (but it would be easy to add). We do support the latter, but only in save-mode, not in send-and-save mode. Until the mode is configurable, you'll need to got to exchange lib.folders.Folder.create_xml()
and change MessageDisposition='SaveOnly'
to MessageDisposition='SendAndSaveCopy'
. Then you can do something like:
email = Message(
subject='Hi',
body='Dear mom',
to_recipients=[Mailbox(email_address='[email protected]')]
)
sent_folder = my_account.folders[Messages][<choose one of the folders here as your destination>]
sent_folder.add_items([email])
from exchangelib.
Since version 1.7.0, sending emails is supported, with less code than the above (example updated for version 1.7.2):
from exchangelib import Account, Credentials
from exchangelib.folders import Message, Mailbox
email = '[email protected]'
password = 'topsecret'
a = Account(email, credentials=Credentials(email, password), autodiscover=True)
# If you don't want a local copy
m = Message(
account=a,
subject='Daily motivation',
body='All bodies are beautiful',
to_recipients=[Mailbox(email_address='[email protected]')]
)
m.send()
# Or, if you want a copy in the 'Sent' folder
m = Message(
account=a,
folder=a.sent,
subject='Daily motivation',
body='All bodies are beautiful',
to_recipients=[Mailbox(email_address='[email protected]')]
)
m.send_and_save()
from exchangelib.
Hello Sir,
How can I attach an excel file from a dir
from exchangelib.
There's an example of file attachments at https://github.com/ecederstrand/exchangelib/blob/master/README.rst#attachments
Excel files are just binary data as far as EWS is concerned, so there's nothing special you need to do there. To get the binary data in a file in Python, do this:
with open('/path/to/file.xlsx', 'rb') as f:
content = f.read()
from exchangelib.
from exchangelib.
Please post your failing code and the except you get.
from exchangelib.
Really appreciate all your help. I think the issue I have is with the .pdf attachment. I am getting a content error, i am not sure its from the FileAttachment in exchangelib . Does it accept PDFs as content?
Error message = TypeError: Field '_content' value '<PyPDF2.pdf.PdfFileReader object at 0x0000000011E320F0>' must be of type <class 'bytes'>
from exchangelib import Account, Credentials, Message, Mailbox, FileAttachment
import PyPDF2
dir_path = ('C:/Users/Istcrmt/Documents/Python/PythonforAnaconda3.5/')
excel_name = 'test.xlsx'
cover_letter_name = 'test.pdf'
#attach an excel file:
with open(dir_path + excel_name, 'rb') as f,
open(dir_path + cover_letter_name, 'rb') as g:
# Read the binary file contents
excel_content = f.read()
cl_content = PyPDF2.PdfFileReader(g)
excel_att = FileAttachment(name='test.xlsx', content=excel_content)
cl_att = FileAttachment(name='test.pdf', content=cl_content)
loop through the email list
for i in email_list.itertuples():
if you want a copy in the 'Sent' folder
m = Message(
account=a
,folder=a.sent
,subject=(i.AGENCY_NAME + ' I made an email script.')
,body=body_of_email
,to_recipients=[Mailbox(email_address=i.NEW_MAIL)])
#attach files
m.attach(excel_att)
m.attach(cl_att)
m.send_and_save()
from exchangelib.
You shouldn't convert g
to a PyPDF2
object. Just read the bytes from the file directly, like you did for the Excel file.
FileAttachment
doesn't care about the file type. It just wants a bytes
object for the content
argument. If the receiving end is having trouble opening the file, you may have to set the content_type
argument explicitly. We try to guess the mimetype of the file from the name
argument, but that may fail.
from exchangelib.
People who come here might be interested in this: https://stackoverflow.com/a/45438104/562769
from exchangelib.
Related Issues (20)
- How to get the itself mailbox and letters from subscription webhook
- Implement SubscribeToAllFolders attribute to subscription requests HOT 3
- about filter specified attachment's name HOT 3
- How to login via UPN or (non-primary) smtp_address?
- can't accces contacts from [email protected] via mailbox delegation(ServiceCredentials) HOT 2
- Reading mime contents of .msg file type attachments HOT 1
- Adding The "X-Auto-Response-Suppress" Header Ignored when sending emails HOT 4
- Creating an Account object raise SSLError with Office 365 account. HOT 1
- Account object doesn't return data of the `primary_smtp_address` mailbox HOT 1
- Getting Invalid Credentials on Live Server(Cpanel) while reading Outlook mailbox but works on local server HOT 3
- MSAL on Office 365 does not describe the way to connect for daemon processes HOT 3
- item.is_read error HOT 6
- `ErrorNonExistentMailbox` in version 5.1.0 but not in 4.8.0 HOT 4
- I want to know if exchangelib python librarie work also with microsoft exchange 2016-2019
- Unable to access in-place archive HOT 8
- Trouble accessing pidTagIconIndex canonical property on message HOT 1
- exchangelib.errors.ErrorFolderNotFound: No subfolder with name '<custom folder>' when using exchangelib 5.1.0 on Ubuntu 20.04 HOT 28
- Unable to join meetings created through CalendarItem. HOT 3
- attached mail as ItemAttachment HOT 10
- Linting error: `account.inbox.filter(...)` HOT 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from exchangelib.