{"id":398,"date":"2017-07-18T18:10:57","date_gmt":"2017-07-18T22:10:57","guid":{"rendered":"http:\/\/www.mbeckler.org\/blog\/?p=398"},"modified":"2018-05-20T22:44:40","modified_gmt":"2018-05-21T02:44:40","slug":"sending-email-from-ubuntu-server-16-04-via-gmail-account","status":"publish","type":"post","link":"https:\/\/www.mbeckler.org\/blog\/?p=398","title":{"rendered":"Sending email from Ubuntu server 16.04 via Gmail account"},"content":{"rendered":"<p>I recently reinstalled my Ubuntu server (16.04 LTS) and wanted to set up the customary unix mail handing stuff so that my cron, temperature monitoring, and hard drive monitor scripts could automatically email me of anything went wrong. In the past I&#8217;ve used mandrill and sparkpost for this, but free services like these keep getting more and more difficult and persnickety to use, most-recently demanding things like opt-out\/unsubscribe links in the emails and adding a privacy policy to the website (despite there not actually _being_ any website). So I wanted to see if it was easy to send email via Gmail&#8217;s SMTP interface.<\/p>\n<p>I followed two nearly-identical guides:<\/p>\n<ul>\n<li><a href=\"http:\/\/www.havetheknowhow.com\/Configure-the-server\/Install-ssmtp.html\">http:\/\/www.havetheknowhow.com\/Configure-the-server\/Install-ssmtp.html<\/a><\/li>\n<li><a href=\"https:\/\/help.ubuntu.com\/community\/EmailAlerts\">https:\/\/help.ubuntu.com\/community\/EmailAlerts<\/a><\/li>\n<\/ul>\n<p>Basically, you need to install the ssmtp package, then edit the ssmtp.conf configuration file:<\/p>\n<p><code>sudo apt-get install ssmtp<\/code><\/p>\n<p><code>sudo vim \/etc\/ssmtp\/ssmtp.conf<\/code><\/p>\n<pre># Config file for sSMTP sendmail\r\n#\r\n# The person who gets all mail for userids < 1000\r\n# Make this empty to disable rewriting.\r\n#root=postmaster\r\nroot=MyEmailAddress@gmail.com\r\n\r\n# The place where the mail goes. The actual machine name is required no\r\n# MX records are consulted. Commonly mailhosts are named mail.domain.com\r\n#mailhub=mail\r\nmailhub=smtp.gmail.com:587\r\n\r\nAuthUser=MyEmailAddress@gmail.com\r\nAuthPass=MyPassword\r\nUseTLS=YES\r\nUseSTARTTLS=YES\r\n\r\n# Where will the mail seem to come from?\r\n#rewriteDomain=\r\nrewriteDomain=gmail.com\r\n\r\n# The full hostname\r\n#hostname=MyMediaServer.home\r\nhostname=MyEmailAddress@gmail.com\r\n\r\n# Are users allowed to set their own From: address?\r\n# YES - Allow the user to specify their own From: address\r\n# NO - Use the system generated From: address\r\nFromLineOverride=YES<\/pre>\n<p>If you have two-factor (2FA) authentication set up for your Google account, you need to create an \"app password\" for this \"device\". Visit this page to create an app password that doesn't use 2FA auth: <a href=\"https:\/\/myaccount.google.com\/apppasswords\">https:\/\/myaccount.google.com\/apppasswords<\/a><\/p>\n<p>However, there is a problem with those two guides, specifically in the <strong>hostname=<\/strong> line. If you put your actual email address as the hostname entry, then google will complain when you try to connect. You can use this test command to see what is going on:<\/p>\n<pre>$ echo \"Hello 1\" | sudo ssmtp -vvv someone@example.org\r\n[< -] 220 smtp.gmail.com ESMTP j76sm1853421ioi.14 - gsmtp\r\n[->] EHLO MyEmailAddress@gmail.com\r\n[< -] 501 5.5.4  https:\/\/support.google.com\/mail\/?p=helo j76sm1853421ioi.14 - gsmtp\r\nssmtp: Cannot open smtp.gmail.com:587<\/pre>\n<p>What you need to do is to set hostname=localhost in the ssmtp.conf file. Then it should work fine.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently reinstalled my Ubuntu server (16.04 LTS) and wanted to set up the customary unix mail handing stuff so that my cron, temperature monitoring, and hard drive monitor scripts could automatically email me of anything went wrong. In the past I&#8217;ve used mandrill and sparkpost for this, but free services like these keep getting [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","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},"jetpack_post_was_ever_published":false},"categories":[1],"tags":[],"class_list":["post-398","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2BznB-6q","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.mbeckler.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/398","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mbeckler.org\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mbeckler.org\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mbeckler.org\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mbeckler.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=398"}],"version-history":[{"count":7,"href":"https:\/\/www.mbeckler.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/398\/revisions"}],"predecessor-version":[{"id":471,"href":"https:\/\/www.mbeckler.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/398\/revisions\/471"}],"wp:attachment":[{"href":"https:\/\/www.mbeckler.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mbeckler.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mbeckler.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}