CoasterKaty / PHPGraphMailer

PHP Class to send email through Microsoft Graph API
https://katystech.blog/2020/08/php-graph-mailer/
GNU General Public License v2.0
33 stars 18 forks source link

Improve handling of undefined variables/properties, prevent PHP notices #9

Closed ElliotNB closed 2 years ago

ElliotNB commented 2 years ago

@CoasterKaty Thank you for putting together this library! Your library and the accompanying blog post saved me quite a bit of time converting a PHP mail handling script that used legacy authentication over to the Microsoft Graph API. I imagine that your library will soon garner a fair bit more attention now that Microsoft is officially disabling legacy authentication on October 1st for existing deployments ( https://techcommunity.microsoft.com/t5/exchange-team-blog/basic-authentication-deprecation-in-exchange-online-september/ba-p/3609437 ).

I fixed a few different spots in the code where undefined variables were generating PHP notices. Some servers have PHP notices turned off, so it's possible that you may not have noticed this (no pun intended) in your own environment.

Here are the notices that I received when running the script unchanged:

Notice: Undefined property: stdClass::$error in /home/..../graph_mailer.php on line 37

Add an isset check on $messageList->error to prevent a PHP notice.

Notice: Undefined variable: attachments in /home/..../graph_mailer.php on line 45

My changes prevent the iteration of the $attachments array when it does not exist (i.e., when no attachments are present).

Notice: Undefined variable: ret in /home/..../graph_mailer.php on line 148

I declared the $ret variable as an empty array so in the event there are no addresses.

Notice: Undefined variable: attachments in /home/..../graph_mailer.php on line 68

I added a ternary operator to supply a null when $attachments is not present.

Please let me know if you have any questions or would like to see any changes!