{"id":478,"date":"2020-10-16T17:01:45","date_gmt":"2020-10-16T17:01:45","guid":{"rendered":"https:\/\/dft.wiki\/?p=478"},"modified":"2026-06-09T13:33:19","modified_gmt":"2026-06-09T17:33:19","slug":"using-msmtp-with-google-smtp-relay-on-ubuntu-20-04","status":"publish","type":"post","link":"https:\/\/dft.wiki\/?p=478","title":{"rendered":"Using MSMTP with Google SMTP Relay on Ubuntu"},"content":{"rendered":"<p>This SMTP application works the same way on Raspbian 10 (Buster).<\/p>\n<pre>sudo apt-get install msmtp msmtp-mta -y\r\nsudo nano \/etc\/msmtprc<\/pre>\n<p>Copy and paste this content to the file:<\/p>\n<pre>defaults\r\nauth on\r\ntls on\r\ntls_trust_file \/etc\/ssl\/certs\/ca-certificates.crt\r\nlogfile ~\/.msmtp.log\r\naccount gmail\r\nhost smtp.gmail.com\r\nport 587\r\nfrom <strong>username<\/strong>@gmail.com\r\nuser <strong>username<\/strong>@gmail.com\r\npassword <strong>password\r\n<\/strong>account default : gmail<\/pre>\n<p>Using your real password is not ideal for two reasons. First, it will be stored as plain text and other users of your server can read it. Second, you have to go into your Google account settings and allow &#8220;less secure apps&#8221; [<a href=\"https:\/\/myaccount.google.com\/lesssecureapps\">Link<\/a>].<\/p>\n<p>Instead, you can create an App Password (a password exclusive to this application) in your Google account, then copy and paste it into the MSMTP configuration file.<\/p>\n<p>Steps (the links are shortcuts; if they do not work, follow the steps manually):<\/p>\n<ul>\n<li>Enter your Google account settings [<a href=\"https:\/\/myaccount.google.com\/\">Link<\/a>].<\/li>\n<li>Select <strong>Security<\/strong> [<a href=\"https:\/\/myaccount.google.com\/security\">Link<\/a>].<\/li>\n<li>Under <strong>Signing in to Google<\/strong>, enable <strong>2-Step Verification<\/strong> [<a href=\"https:\/\/myaccount.google.com\/signinoptions\/two-step-verification?gar=1\">Link<\/a>].<\/li>\n<li>Select <strong>App Passwords<\/strong> [<a href=\"https:\/\/myaccount.google.com\/apppasswords?gar=1\">Link<\/a>].<\/li>\n<li>Choose the <strong>app<\/strong> and <strong>device<\/strong> you want to generate the password for.<\/li>\n<li>Click <strong>Generate<\/strong>.<\/li>\n<li><strong>Copy the password<\/strong>, which will look something like this: mssl uemq sabi fjbc.<\/li>\n<li><strong>Paste it into the MSMTP<\/strong> configuration file.<\/li>\n<\/ul>\n<p>This App Password procedure also applies if you want to use Sendmail or Postfix as a Google relay.<\/p>\n<p>Note that all emails sent through the Google server will use your Google account address as the sender.<\/p>\n<p>Test your configuration:<\/p>\n<pre>(echo \"Subject: Test\"; echo 'Body of the email') | msmtp <strong>username<\/strong>@gmail.com\r\ncat file.txt | msmtp <span style=\"color: #ff0000;\"><strong>-a<\/strong><\/span> default <strong>username<\/strong>@gmail.com<\/pre>\n<p>Note: <span style=\"color: #ff0000;\"><strong>-a<\/strong><\/span> followed by a configuration name will select which configuration (server, credentials, etc.) to use.<\/p>\n<p>You can also install the mail tools and test them:<\/p>\n<pre>sudo apt-get install mailutils -y\r\necho 'Message' | mail -s \"Subject\" <strong>username<\/strong>@gmail.com\r\necho 'Message' | mail -s \"Subject\" -A attachment.zip <strong>username<\/strong>@gmail.com\r\necho 'Message' | sendmail <strong>username<\/strong>@gmail.com<\/pre>\n<hr \/>\n<p><strong>BONUS<\/strong><\/p>\n<p>Sending test emails with CURL:<\/p>\n<pre>curl --url 'smtp:\/\/smtp.example.com:25' --mail-from 'from_user@example.com' --mail-rcpt 'to_user@example.com' -F '='<\/pre>\n<p>Sending test emails with Telnet\/Netcat:<\/p>\n<pre>telnet smtp.example.com 25<\/pre>\n<pre>EHLO example.com\r\nMAIL FROM:&lt;from_user@example.com&gt;\r\nRCPT TO:&lt;to_user@example.com&gt;\r\nDATA\r\nSubject: Test Email Subject\r\nFrom: from_user@example.com\r\nTo: to_user@example.com\r\n\r\nBody of the email\r\n.\r\nQUIT<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This SMTP application works the same way on Raspbian 10 (Buster). sudo apt-get install msmtp [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,6,7],"tags":[],"class_list":["post-478","post","type-post","status-publish","format-standard","hentry","category-linux","category-raspberry-pi","category-web"],"_links":{"self":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/478","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=478"}],"version-history":[{"count":12,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/478\/revisions"}],"predecessor-version":[{"id":5819,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/478\/revisions\/5819"}],"wp:attachment":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}