Haven't looked at your code and I'm not sure if this will be the right rabbit hole to go down but gmail is a pain on the SMTP side (they changed something a few years back). I had a lot of fun fixing the email feature here on theSwamp server. I think it found I needed port 465 at ssl://smtp.gmail...
MS Exchange related: Also, I had some problems with email automation once (CAD Mgr stuff) and instead of using the <username>@domain@smtp.office... url I just used the same SMTP URL as one of the printers in the office (I didn't tell any of the bosses--or IT--about how I got that stuff to work though!
And I imagine if that company ever replaced me, my predecessor may have been a bit nervous if (s)he found that little gem. haha!). I just sent something to myself from a printer and tracked down the URL from the email headers.
-i.e. your problem is probably your password (the port you send with) and ssl. Try port 465 because I think it starts ssl before authentication. If I remember, 587 starts ssl after auth.
Good luck.