vgrem / phpSPO

Microsoft 365 Library for PHP.
MIT License
357 stars 115 forks source link

Sending mail with attachments #301

Closed DavidBrogli closed 1 year ago

DavidBrogli commented 1 year ago

Hello together

I am trying to find my way around and am having problems. Sending mail works, but when I want to add an attachment, the mail arrives without an attachment. Has this functionality ever worked in this respo?

Attached is my code.

    function SendMail($to = [], $cc = [], $title, $body, $attachments = [], $fromMailbox = "buchhaltung@mail.com")
    {
        $message = $this->client->getUsers()->getById($fromMailbox)->getMessages()->createType();

        $message->setSubject($title);
        $message->setBody(new ItemBody(BodyType::HTML, $body));

        foreach($to as $mailaddr)
            $message->setToRecipients([new EmailAddress(null,$mailaddr)]);

        foreach($cc as $mailaddr)
            $message->setCcRecipients([new EmailAddress(null,$mailaddr)]);

        $attachment = $message->addAttachment(FileAttachment::class);
        $attachment->setContentBytes("bWFjIGFuZCBjaGVlc2UgdG9kYXk=");
        $attachment->setIsInline(false);
         $attachment->setName("Test.txt");

        $this->client->getUsers()->getById($fromMailbox)->sendEmail($message,true)->executeQuery();
    }
DavidBrogli commented 1 year ago

@vgrem No reaction? Is tagging tickets the only task currently performed?

vgrem commented 1 year ago

Greetings,

thank you for catching this issue! Indeed in this case attachments never gets marked as serializable and therefore not included in message payload while calling sendEmail method (the latest commit should fix this error)

And before the next version gets released, the following patch for addAttachment method in of Message class should do the trick:

if($this->getAttachments()->getCount() === 0){
            $this->setProperty("Attachments", $this->getAttachments());
        }