microsoftgraph / msgraph-sdk-php

Microsoft Graph Library for PHP.
Other
566 stars 143 forks source link

How to send messages in MIME format? #1470

Open GenieTim opened 6 months ago

GenieTim commented 6 months ago

The documentation asserts that the Graph API supports sending E-Mails using the MIME format.

While previous versions of this SDK still gave access to simply building a new request to whatever URL, the current version is not as simple anymore.

May I kindly ask for some guidance, on how to send E-Mails in the MIME format using the current version of this SDK?

francescodiperna commented 5 months ago

Good morning @GenieTim , did you implement the send in mime format? We have implemented but recently we have a problem with the versione 1.110

GenieTim commented 5 months ago

No, sorry, @francescodiperna , I did not. Would still be thankful for pointers, whether there is, or will be, a possibility to do it with this library, or whether I have to do it "raw"

francescodiperna commented 5 months ago

Good morning, @GenieTim. I did, using the versione 1.110, below the code.


 private function inviaEmailMicrosoft($mimeContent, $username)
    {        
        $requestUrl = '/users/' . $username . '/sendMail';        
        $emailResponse = $this->graphClient->createRequest('POST', $requestUrl)
            ->addHeaders(["Content-Type" => "text/plain"])
            ->attachBody(base64_encode(serialize([
                "Content-Type" => "text/plain",
                'message' => $mimeContent
            ])))->execute();
        if ($emailResponse->getStatus() == 400) {
            throw new \Exception("ATTENZIONE! impossibile inviare il messaggio dell'utente " . $username .
                "Invalid base64 string for MIME content");
        }
        if ($emailResponse->getStatus() != 202) {
            throw new \Exception("ATTENZIONE! impossibile inviare il messaggio dell'utente " . $username);
        }

        return true;
    }