{"id":2170,"date":"2019-01-23T22:24:39","date_gmt":"2019-01-24T04:24:39","guid":{"rendered":"http:\/\/thepizzy.net\/blog\/?p=2170"},"modified":"2019-01-24T09:52:23","modified_gmt":"2019-01-24T15:52:23","slug":"connecting-to-gmail-via-php","status":"publish","type":"post","link":"https:\/\/thepizzy.net\/blog\/2019\/01\/connecting-to-gmail-via-php\/","title":{"rendered":"Connecting to Gmail via PHP"},"content":{"rendered":"\n<p>I have just spent several hours tracking down a ton of loose ends to figure out just why my PHP application (in my case, phpBB 3.2.5) wouldn&#8217;t connect to a Gmail account.<\/p>\n\n\n\n<p>In the end, it likely has to do with something on Google&#8217;s end, like:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Enabling &#8220;<a href=\"https:\/\/support.google.com\/accounts\/answer\/6010255\/?hl=en&amp;authuser=1\">Letting Less Secure Apps access your account<\/a>&#8220;<\/li><li>Using the correct <a href=\"https:\/\/support.google.com\/mail\/answer\/7126229?authuser=1&amp;visit_id=636838920295009321-957769219&amp;hl=en&amp;rd=1\">Gmail IMAP\/SMTP settings<\/a><\/li><li>Or a combination of &#8220;<a href=\"https:\/\/support.google.com\/accounts\/answer\/185839?hl=en&amp;authuser=1\">Turning on 2-Step Verification<\/a>&#8220;, so that you can then generate and &#8220;<a href=\"https:\/\/support.google.com\/accounts\/answer\/185833?hl=en&amp;authuser=1\">Sign in using App Passwords<\/a>&#8220;<\/li><\/ul>\n\n\n\n<p>My approach to solving the issue took into account all of these on the way to the final solution:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>An <strong>\/etc\/hosts<\/strong> entry for <strong>smtp.gmail.com<\/strong><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code># This is just 1 of 2 IPv4 servers for the domain\n173.194.208.108 smtp.gmail.com<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Diagnosis<\/h2>\n\n\n\n<p>In phpBB&#8217;s &#8220;Admin Control Panel,&#8221; there is an Errors page that would constantly spit out a &#8220;timeout&#8221; error no matter what settings I used.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Email error\n\u00c2\u00bb EMAIL\/SMTP\n\/phorums\/memberlist.php\n\nCould not connect to smtp host : 110 : Connection timed out\n\nErrno 2: stream_socket_client(): unable to connect to smtp.gmail.com:465 (Connection timed out) at [ROOT]\/includes\/functions_messenger.php line 1188<\/code><\/pre>\n\n\n\n<p>The email settings I have in place are related to a couple things:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>I own several domains, the one for this purpose is: the-spot.net<\/li><li>I am grandfathered into the original Google Apps Suite, and I have that attached to the domain.<\/li><li>All email addresses are routed to a catch-all address with an long-random-alphanumeric account name, so when I need to send from a specific account, I have to create a new User in Google Apps Admin Console.<\/li><li>As the Admin User, I also had to:<ul><li>Enable users to set up 2-Step Verification<\/li><li>Enable users to set their own &#8220;Less Secure Apps&#8221; access setting<\/li><\/ul><\/li><li>Then as the Newly Created User, I had to:<ul><li>Log into the account for the first time, and create a[nother] password<\/li><li>Turn on &#8220;Less Secure Apps&#8221; access <\/li><li>Set up 2-Step Verification (I used my Google Voice number, as I have with all my Google Accounts)<\/li><li>Generate an App Password.<\/li><\/ul><\/li><\/ul>\n\n\n\n<p>Once I had followed all those account-configuration steps, I then referenced the <a href=\"https:\/\/support.google.com\/mail\/answer\/7126229?authuser=1&amp;visit_id=636838920295009321-957769219&amp;hl=en&amp;rd=1\">IMAP\/SMTP Settings<\/a> documentation, and started entering the values into the fields, as shown.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/i0.wp.com\/thepizzy.net\/blog\/wp-content\/uploads\/2019\/01\/phpbb-email-settings.png.jpg?ssl=1\" target=\"_blank\" rel=\"noreferrer noopener\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"580\" height=\"505\" data-attachment-id=\"2179\" data-permalink=\"https:\/\/thepizzy.net\/blog\/2019\/01\/connecting-to-gmail-via-php\/phpbb-email-settings-png\/\" data-orig-file=\"https:\/\/i0.wp.com\/thepizzy.net\/blog\/wp-content\/uploads\/2019\/01\/phpbb-email-settings.png.jpg?fit=965%2C840&amp;ssl=1\" data-orig-size=\"965,840\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"phpbb-email-settings.png\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/thepizzy.net\/blog\/wp-content\/uploads\/2019\/01\/phpbb-email-settings.png.jpg?fit=580%2C505&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/thepizzy.net\/blog\/wp-content\/uploads\/2019\/01\/phpbb-email-settings.png.jpg?resize=580%2C505&#038;ssl=1\" alt=\"\" class=\"wp-image-2179\" srcset=\"https:\/\/i0.wp.com\/thepizzy.net\/blog\/wp-content\/uploads\/2019\/01\/phpbb-email-settings.png.jpg?w=965&amp;ssl=1 965w, https:\/\/i0.wp.com\/thepizzy.net\/blog\/wp-content\/uploads\/2019\/01\/phpbb-email-settings.png.jpg?resize=300%2C261&amp;ssl=1 300w, https:\/\/i0.wp.com\/thepizzy.net\/blog\/wp-content\/uploads\/2019\/01\/phpbb-email-settings.png.jpg?resize=150%2C131&amp;ssl=1 150w, https:\/\/i0.wp.com\/thepizzy.net\/blog\/wp-content\/uploads\/2019\/01\/phpbb-email-settings.png.jpg?resize=768%2C669&amp;ssl=1 768w, https:\/\/i0.wp.com\/thepizzy.net\/blog\/wp-content\/uploads\/2019\/01\/phpbb-email-settings.png.jpg?resize=700%2C609&amp;ssl=1 700w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><figcaption>phpBB 3.2.5 &gt; General &gt; Email Settings pane<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Testing<\/h2>\n\n\n\n<p>Obviously, the settings in the image above did not work. They continually provided the same &#8220;Connection Timeout&#8221; error mentioned earlier.<\/p>\n\n\n\n<p>In research and trying to test whether or not my settings were correct, I saw some mention that&#8230;<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>&#8230;the &#8220;Send a test email&#8221; button doesn&#8217;t work, and you have to send either an email from the User&#8217;s Profile, or a Private Message.<\/p><cite>Some random reply on one of the many support threads<\/cite><\/blockquote>\n\n\n\n<p>A little foreshadowing here&#8230; <strong>that is not true<\/strong>. The button works fine.<\/p>\n\n\n\n<figure class=\"wp-block-pullquote alignleft\"><blockquote><p>The real solution comes down to whether you can make \/etc\/host changes.<\/p><cite>Me<br>Saying: &#8220;You might be fucked.&#8221;<\/cite><\/blockquote><\/figure>\n\n\n\n<p>If you <strong>do<\/strong> have command-line access, and\/or can make changes to the <strong>\/etc\/hosts<\/strong> file that will stick &#8211; i.e. you have CLI root access, or you have a cPanel interface to set your own local dns entries, then you&#8217;re in luck.<\/p>\n\n\n\n<p>To see what is going on, get to your web server, and run the following command. You&#8217;ll see what IP is initially connected to when trying to telnet into the email server at the tls port:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>root@web:\/home\/web# telnet smtp.gmail.com 587\nTrying 2607:f8b0:400d:c0a::6d...<\/code><\/pre>\n\n\n\n<p>It&#8217;s an IPv6. That&#8217;s unexpected&#8230;but I recall <a href=\"https:\/\/publicpolicy.googleblog.com\/2011\/02\/ipv6-marks-next-chapter-in-history-of.html\">Google announcing<\/a> they are making as many of their services prefer IPv6 where supported. (The link is the best I could find&#8230;the original announcement is gone.)<\/p>\n\n\n\n<p>So there&#8217;s the problem&#8230; the PHP socket stuff is having a hard time with the IPv6 connection.<\/p>\n\n\n\n<p>Now, if you run the following command from the web server&#8217;s terminal, you&#8217;ll see all the things that are related to the smtp.gmail.com domain:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>root@web:\/home\/web# dig +short smtp.gmail.com\ngmail-smtp-msa.l.google.com.\n173.194.208.108\n173.194.208.109<\/code><\/pre>\n\n\n\n<p>Two of those are IP addresses. One of them is a domain, and is of little use to us: we already have the smtp.gmail.com domain, and we can&#8217;t put a domain into the left-side of the hosts file.<\/p>\n\n\n\n<p>Take either of the IP addresses, and insert them into your \/etc\/hosts file&#8230;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>root@web:\/home\/web# sudo vi \/etc\/hosts\n### \n173.194.208.108 smtp.gmail.com\n:wq<\/code><\/pre>\n\n\n\n<p>Save the file, go back to the phpBB interface and make sure:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Use STMP server for email: <strong>yes<\/strong><\/li><li>Your password is an App Password<\/li><li>Your username, from, reply-to, and other email addresses are the Google Account for the App Password<\/li><li>The domain is: <strong>ssl:\/\/smtp.gmail.com<\/strong> (you might be able to use <strong>tls:\/\/<\/strong>)<\/li><li>The port is: <strong>465<\/strong> (you might be able to use <strong>587<\/strong> for TLS)<\/li><li>Verify SSL Cert: <strong>yes<\/strong><\/li><li>Verify SMTP Peer: <strong>yes<\/strong><\/li><\/ul>\n\n\n\n<p>Then hit the &#8220;Send a test email&#8221; button in the top section. Once you do, you should get: a fairly quick green success message page, no errors in the log, and an email that arrives to the user account you are currently logged into.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"580\" height=\"342\" data-attachment-id=\"2184\" data-permalink=\"https:\/\/thepizzy.net\/blog\/2019\/01\/connecting-to-gmail-via-php\/phpbb-test-email-png\/\" data-orig-file=\"https:\/\/i0.wp.com\/thepizzy.net\/blog\/wp-content\/uploads\/2019\/01\/phpbb-test-email.png.jpg?fit=637%2C376&amp;ssl=1\" data-orig-size=\"637,376\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"phpbb-test-email.png\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/thepizzy.net\/blog\/wp-content\/uploads\/2019\/01\/phpbb-test-email.png.jpg?fit=580%2C342&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/thepizzy.net\/blog\/wp-content\/uploads\/2019\/01\/phpbb-test-email.png.jpg?resize=580%2C342&#038;ssl=1\" alt=\"\" class=\"wp-image-2184\" srcset=\"https:\/\/i0.wp.com\/thepizzy.net\/blog\/wp-content\/uploads\/2019\/01\/phpbb-test-email.png.jpg?w=637&amp;ssl=1 637w, https:\/\/i0.wp.com\/thepizzy.net\/blog\/wp-content\/uploads\/2019\/01\/phpbb-test-email.png.jpg?resize=300%2C177&amp;ssl=1 300w, https:\/\/i0.wp.com\/thepizzy.net\/blog\/wp-content\/uploads\/2019\/01\/phpbb-test-email.png.jpg?resize=150%2C89&amp;ssl=1 150w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><figcaption>It worked. Finally.<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Alternatives<\/h2>\n\n\n\n<p>I haven&#8217;t tried some of these alternatives, because I was able to get it working via the method above. However, if you <strong>can&#8217;t<\/strong> make changes to the \/etc\/hosts file, perhaps try some of the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Create a DNS entry that points a subdomain you can control to the IP (or if possible, both IPs) for the smtp.gmail.com server.<\/li><li>Create an email account via the webhost\/cpanel interface, or maybe even the domain registrar, and go the self-hosted route. At least it&#8217;ll be fully under your control, and you can IMAP the account into another Gmail account, and tag those messages via Filters, if you need to receive them.<ul><li>This is the solution I originally used, until I moved my domain over to Google Apps, causing this problem in the first place.<\/li><\/ul><\/li><li>Or as a last resort, change the code making the request to use a PHP library that will support making calls directly to the original domain. There are many support threads about this problem that end with simply changing the properties sent to the mailer class.<\/li><\/ul>\n\n\n\n<p>Let me know in the comments if you have found a different solution, or see problems with my approach (other than the obvious and expected &#8220;Unexpected IP Changes&#8221; or &#8220;Single IP, no redundancy&#8221;).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How I ended up fixing phpBB&#8217;s connection to smtp.gmail.com, and getting the-spot.net&#8217;s email back online.<\/p>\n","protected":false},"author":1,"featured_media":2177,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"Connecting to Gmail in phpBB","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[910,3,9,347],"tags":[966,968,350,964,152,47,965,967,969],"class_list":["post-2170","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tech-fixes","category-tech","category-the-spot-net","category-the-spot-network","tag-connection-timed-out","tag-email-error","tag-forums","tag-gmail","tag-php","tag-phpbb","tag-smtp-gmail-com","tag-stream_socket_client","tag-unable-to-connect-to-smtp-gmail-com"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/thepizzy.net\/blog\/wp-content\/uploads\/2019\/01\/splash-1.png?fit=1980%2C960&ssl=1","jetpack_likes_enabled":false,"jetpack_shortlink":"https:\/\/wp.me\/prOO4-z0","jetpack-related-posts":[{"id":2236,"url":"https:\/\/thepizzy.net\/blog\/2020\/01\/phpbb-extension-template-partials\/","url_meta":{"origin":2170,"position":0},"title":"phpBB extension Template Partials","author":"[[Neo]]","date":"January 19, 2020","format":false,"excerpt":"I've been working on replacing the front-end for the-spot.net and thus, the front-end of phpBB forums, using only the phpBB extension framework, trying to avoid having to put custom code into the core files, as was required pre-3.2 for some of my features. It turns out that the existing topics\u2026","rel":"","context":"In &quot;How To&quot;","block_context":{"text":"How To","link":"https:\/\/thepizzy.net\/blog\/category\/how-to\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":91,"url":"https:\/\/thepizzy.net\/blog\/2007\/05\/update-new-rss-code-in-the-works\/","url_meta":{"origin":2170,"position":1},"title":"Update: New RSS Code in the works.","author":"[[Neo]]","date":"May 28, 2007","format":false,"excerpt":"Earlier I posted on the RSS Code that I'm working on. I've done quite a bit of work on it so far - learning about the various functions that PHP offers. Most of what I thought I was learning while setting up the forums was not actually PHP's functions, they\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/thepizzy.net\/blog\/category\/tech\/programming\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/thepizzy.net\/blog\/wp-content\/uploads\/2008\/04\/Untitled-2.png?fit=400%2C400&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":100,"url":"https:\/\/thepizzy.net\/blog\/2008\/01\/twitter-tsn7\/","url_meta":{"origin":2170,"position":2},"title":"twitter &#038; tsn.7","author":"[[Neo]]","date":"January 30, 2008","format":false,"excerpt":"With a new My Spot page comes new possibilities. This time around, I'm going to make a concerted effort to actually write in new code and attach a new database backend that pulls from the phpbb database and the users' accounts on the-spot.net.With this new plan, there will be increased\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/thepizzy.net\/blog\/category\/tech\/programming\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/thepizzy.net\/blog\/wp-content\/uploads\/2008\/04\/Untitled-2.png?fit=400%2C400&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":1518,"url":"https:\/\/thepizzy.net\/blog\/2014\/03\/tsn8-in-the-works\/","url_meta":{"origin":2170,"position":3},"title":"tsn8 in the works","author":"[[Neo]]","date":"March 18, 2014","format":false,"excerpt":"Development of tsn8 has begun, with better technology, a new user experience, social media integration, and a planned release of Summer 2014!","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/thepizzy.net\/blog\/category\/tech\/programming\/"},"img":{"alt_text":"tsn logo","src":"https:\/\/i0.wp.com\/thepizzy.net\/blog\/wp-content\/uploads\/2014\/03\/tsnlogo-featured.png?fit=690%2C405&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/thepizzy.net\/blog\/wp-content\/uploads\/2014\/03\/tsnlogo-featured.png?fit=690%2C405&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/thepizzy.net\/blog\/wp-content\/uploads\/2014\/03\/tsnlogo-featured.png?fit=690%2C405&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":56,"url":"https:\/\/thepizzy.net\/blog\/2006\/05\/were-number-1\/","url_meta":{"origin":2170,"position":4},"title":"&#8220;We&#8217;re number 1!!&#8221;","author":"[[Neo]]","date":"May 16, 2006","format":false,"excerpt":"Ok, so while coding the tsnX forums for the-spot.net, there were several instances in which the mods didn't work because everyone's mods assume (usually) that they are going to be the only mod installed in the forums. And while this is the ideology that saves time when writing a modification,\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/thepizzy.net\/blog\/category\/tech\/programming\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1116,"url":"https:\/\/thepizzy.net\/blog\/2011\/02\/how-to-connect-to-sql-through-windows-authenticated-odbc-in-php\/","url_meta":{"origin":2170,"position":5},"title":"How to Connect to SQL through Windows Authenticated ODBC in PHP","author":"[[Neo]]","date":"February 6, 2011","format":false,"excerpt":"If you use MSSQL over ODBC, connecting w\/ PHP is simply a DOMAIN\\WEBSERVER acct on the MSSQL server & the DSN in PHP's odbc_connect()","rel":"","context":"In &quot;How To&quot;","block_context":{"text":"How To","link":"https:\/\/thepizzy.net\/blog\/category\/how-to\/"},"img":{"alt_text":"ODBC Connection","src":"https:\/\/i0.wp.com\/thepizzy.net\/blog\/wp-content\/uploads\/2011\/02\/8787717937219497341-150x150.png?resize=350%2C200","width":350,"height":200},"classes":[]}],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/thepizzy.net\/blog\/wp-json\/wp\/v2\/posts\/2170","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thepizzy.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thepizzy.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thepizzy.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thepizzy.net\/blog\/wp-json\/wp\/v2\/comments?post=2170"}],"version-history":[{"count":12,"href":"https:\/\/thepizzy.net\/blog\/wp-json\/wp\/v2\/posts\/2170\/revisions"}],"predecessor-version":[{"id":2191,"href":"https:\/\/thepizzy.net\/blog\/wp-json\/wp\/v2\/posts\/2170\/revisions\/2191"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/thepizzy.net\/blog\/wp-json\/wp\/v2\/media\/2177"}],"wp:attachment":[{"href":"https:\/\/thepizzy.net\/blog\/wp-json\/wp\/v2\/media?parent=2170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thepizzy.net\/blog\/wp-json\/wp\/v2\/categories?post=2170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thepizzy.net\/blog\/wp-json\/wp\/v2\/tags?post=2170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}