Giter VIP home page Giter VIP logo

Comments (10)

rm17tink avatar rm17tink commented on June 5, 2024 1

from exchangelib.

ecederstrand avatar ecederstrand commented on June 5, 2024

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.

ecederstrand avatar ecederstrand commented on June 5, 2024

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.

rm17tink avatar rm17tink commented on June 5, 2024

Hello Sir,
How can I attach an excel file from a dir

from exchangelib.

ecederstrand avatar ecederstrand commented on June 5, 2024

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.

rm17tink avatar rm17tink commented on June 5, 2024

from exchangelib.

ecederstrand avatar ecederstrand commented on June 5, 2024

Please post your failing code and the except you get.

from exchangelib.

rm17tink avatar rm17tink commented on June 5, 2024

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.

ecederstrand avatar ecederstrand commented on June 5, 2024

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.

MartinThoma avatar MartinThoma commented on June 5, 2024

People who come here might be interested in this: https://stackoverflow.com/a/45438104/562769

from exchangelib.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.