Не удается установить получателей — O365

#python #office365api

#python #office365api

Вопрос:

Я пытаюсь отправить электронное письмо, используя O365

Однако я не смог найти способ установить получателя без доступа к приватному атрибуту.

 from O365 import *

my_protocol = MSGraphProtocol(api_version='beta')
account = Account(
    credentials=('id', 'id'),
    protocol=my_protocol
)
if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    account.authenticate(scopes=['mailbox', 'message_send'])

msg = Message(parent=account)
msg.body = 'Hi, foobar.'
msg.subject = 'Bar Foo'

msg.to = Recipient(address='foobar@outlook.com', name='lucas') # dont work
msg._Message__to._recipients = [Recipient(address='foobar@outlook.com', name='lucas')] # works but very bad way i supossed
msg.setRecipients(Recipient(address='foobar@outlook.com', name='lucas')) # some old tutorials used this, but dont work either

msg.send()
  

Это, должно быть, очень глупый вопрос, но я прочитал классы из документа и не смог найти установщик для получателя.

Спасибо!

Ответ №1:

Нашел это в github.

 msg.to.add('email')
  

Эта функция добавляет получателя. Или много, если вы передаете список строк.

Окончательный код:

 from O365 import *

my_protocol = MSGraphProtocol(api_version='beta')
account = Account(
    credentials=('id', 'id'),
    protocol=my_protocol
)
if not account.is_authenticated:  # will check if there is a token and has not expired
    # ask for a login
    account.authenticate(scopes=['mailbox', 'message_send'])

msg = Message(parent=account)
msg.body = 'Hi, foobar.'
msg.subject = 'Bar Foo'

msg.to.add('foobar@outlook.com')

msg.send()