{"id":64419,"date":"2022-05-17T09:00:48","date_gmt":"2022-05-17T09:00:48","guid":{"rendered":"https:\/\/www.cryptocabaret.com\/?p=64419"},"modified":"2022-05-17T09:00:48","modified_gmt":"2022-05-17T09:00:48","slug":"travel-off-the-grid-and-still-send-emails-with-putmail","status":"publish","type":"post","link":"https:\/\/www.cryptocabaret.com\/?p=64419","title":{"rendered":"Travel off the grid and still send emails with putmail"},"content":{"rendered":"<p><span class=\"field field--name-title field--type-string field--label-hidden\">Travel off the grid and still send emails with putmail<\/span><br \/>\n<span class=\"field field--name-uid field--type-entity-reference field--label-hidden\"><a title=\"View user profile.\" href=\"https:\/\/opensource.com\/users\/ksonney\" class=\"username\">Kevin Sonney<\/a><\/span><br \/>\n<span class=\"field field--name-created field--type-created field--label-hidden\">Tue, 05\/17\/2022 &#8211; 03:00<\/span><\/p>\n<div data-drupal-selector=\"rate-node-70021\" class=\"rate-widget-thumbs-up\" title=\"Register or Login to like.\">\n<div class=\"rate-thumbs-up-btn-up vote-pending\"><a href=\"https:\/\/opensource.com\/user\/register\">Register<\/a> or <a href=\"https:\/\/opensource.com\/user\/login?current=\/rss.xml\">Login<\/a> to like<\/div>\n<div class=\"rate-score\"><a href=\"https:\/\/opensource.com\/user\/register\">Register<\/a> or <a href=\"https:\/\/opensource.com\/user\/login?current=\/rss.xml\">Login<\/a> to like<\/div>\n<\/div>\n<div class=\"clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item\">\n<p><em>Automation is a hot topic right now. In my day job as an SRE part of my remit is to automate as many repeating tasks as possible. But how many of us do that in our daily, not-work, lives? This year, I am focused on automating away the toil so that we can focus on the things that are important.<\/em><\/p>\n<p>In &#8220;normal times&#8221; my wife and I travel a lot. And as anyone who similarly travels a lot knows, connectivity can be very expensive. We were recently on a cruise and the &#8220;Premium&#8221; ship-board internet cost upwards of $200 for the full 7 days, and even then it had all the drawbacks of satellite internet in terms of speed and congestion. So to make my life easier, I set up <a href=\"https:\/\/www.offlineimap.org\/\">offlineimap<\/a> to download my mail, <a href=\"https:\/\/www.dovecot.org\/\">dovecot<\/a> to let me use an IMAP client to read my mail, and <a href=\"https:\/\/marlam.de\/msmtp\/\">msmtp<\/a> to send mail from my various accounts. I covered much of <a href=\"https:\/\/opensource.com\/article\/20\/1\/sync-email-offlineimap\">this setup in 2020<\/a>, and the rest is documented in many places online.<\/p>\n<\/p>\n<div class=\"embedded-resource-list callout-float-right\">\n<div class=\"field field--name-title field--type-string field--label-hidden field__item\">More great content<\/div>\n<div class=\"field field--name-links field--type-link field--label-hidden field__items\">\n<div class=\"field__item\"><a href=\"https:\/\/www.redhat.com\/en\/services\/training\/rh024-red-hat-linux-technical-overview?intcmp=7016000000127cYAAQ\">Free online course: RHEL technical overview<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/developers.redhat.com\/cheat-sheets\/advanced-linux-commands\/?intcmp=7016000000127cYAAQ\">Learn advanced Linux commands<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/downloads\/cheat-sheets?intcmp=7016000000127cYAAQ\">Download cheat sheets<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/tags\/linux?intcmp=7016000000127cYAAQ\">Find an open source alternative<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/resources?intcmp=7016000000127cYAAQ\">Explore open source resources<\/a><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<p>What is not often discussed is that you still need to be online to send mail. The most common recommended solution for sending mail is msmtp, and it always rejects mail if it can&#8217;t connect to the desired host. But it does have a daemon option (<code>msmtpd<\/code>) that can be used to accept local SMTP connections and forward them to another program. By default, this is <code>msmtp<\/code> itself. But again, I don&#8217;t want to send things now, I want to send things when I have a connection, which is when I stumbled upon <a href=\"https:\/\/github.com\/tgray\/putmail\">putmail<\/a>.<\/p>\n<p><code>Putmail<\/code> is a set of Python scripts that sends mail to pre-configured smtp servers based on the <code>From<\/code> address in a mail message. It is stable (unmodified since 2011), and just works. If you, like me, have multiple email addresses which have to be sent via multiple SMTP relays, this is just the thing you need. Each email address you send from has its own configuration file, and since <code>putmail<\/code> decides on which to use based on the message itself, there&#8217;s no need to have to set up multiple sending setups in a single mail client.<\/p>\n<p>As an example, to send with a <code>gmail<\/code> account, you would create the file <code>.putmail\/yourname@gmail.com<\/code> and fill in the following information.<\/p>\n<pre>\n<div class=\"geshifilter\"><div class=\"bash geshifilter-bash\">yourname<span class=\"sy0\">@<\/span>gmail.com putmail configuration<br><span class=\"br0\">[<\/span>config<span class=\"br0\">]<\/span><br>\nemail = yourname<span class=\"sy0\">@<\/span>gmail.com<br>\nserver = smtp.gmail.com<br>\nport = <span class=\"nu0\">587<\/span><br>\nusername = yourname<span class=\"sy0\">@<\/span>gmail.com<br>\npassword = XXXXXXXXXXXXXXXXX<br>\ntls = on<\/div><\/div><\/pre>\n<p>And that&#8217;s it. Configure a mail client to send via <code>putmail.py<\/code> instead of <code>sendmail<\/code> or <code>msmtp<\/code> and send a message.<\/p>\n<p>The next best things are the <code>putmail_enque.py<\/code> and <code>putmail_dequeue.py<\/code> scripts. The first takes an email and stores it to send later. The second loops through the queue and delivers the mail. By specifying <code>putmail_enqueue.py<\/code> as the program <code>msmtpd<\/code> runs, I can &#8220;send&#8221; an email now, and it just waits for me to run <code>putmail_dequeue.py<\/code> later. Here is my <code>msmtpd<\/code> startup command, specifying <code>putmail_enqueue.py<\/code> as the item to be used for mail delivery.<\/p>\n<pre>\n<div class=\"geshifilter\"><div class=\"bash geshifilter-bash\">msmtpd <span class=\"re5\">--port<\/span>=<span class=\"nu0\">1025<\/span> <span class=\"re5\">--log<\/span>=<span class=\"sy0\">\/<\/span>tmp<span class=\"sy0\">\/<\/span>msmtpd.log <span class=\"re5\">--command<\/span>=<span class=\"st_h\">'putmail_enqueue.py -f %F'<\/span> -<\/div><\/div><\/pre>\n<p>I use the following script as a <code>presynchook<\/code> in <code>offlineimap<\/code> to check to see if I am connected, and if so, send mail.<\/p>\n<pre>\n<div class=\"geshifilter\"><div class=\"bash geshifilter-bash\"><span class=\"co0\">#!\/bin\/bash<\/span><br><span class=\"kw3\">echo<\/span> Sending queued messages <span class=\"br0\">(<\/span><span class=\"kw1\">if<\/span> any<span class=\"br0\">)<\/span><br><span class=\"re2\">QUEUEDMAIL<\/span>=$<span class=\"br0\">(<\/span><span class=\"kw2\">find<\/span> <span class=\"re1\">$HOME<\/span><span class=\"sy0\">\/<\/span>.putmail<span class=\"sy0\">\/<\/span>queue <span class=\"re5\">-type<\/span> f <span class=\"sy0\">|<\/span> <span class=\"kw2\">wc<\/span> -l<span class=\"br0\">)<\/span><br><span class=\"kw1\">if<\/span> <span class=\"br0\">[<\/span> <span class=\"re1\">$QUEUEDMAIL<\/span> <span class=\"re5\">-ne<\/span> <span class=\"nu0\">0<\/span> <span class=\"br0\">]<\/span>; <span class=\"kw1\">then<\/span><br>\n\u00a0 <span class=\"kw2\">ping<\/span> <span class=\"re5\">-n<\/span> <span class=\"re5\">-c<\/span> <span class=\"nu0\">1<\/span> imap.gmail.com <span class=\"sy0\">&gt;\/<\/span>dev<span class=\"sy0\">\/<\/span>null <span class=\"nu0\">2<\/span><span class=\"sy0\">&gt;\/<\/span>dev<span class=\"sy0\">\/<\/span>null<br>\n\u00a0 <span class=\"kw1\">if<\/span> <span class=\"br0\">[<\/span> <span class=\"re4\">$?<\/span> <span class=\"re5\">-eq<\/span> <span class=\"nu0\">0<\/span> <span class=\"br0\">]<\/span>; <span class=\"kw1\">then<\/span><br>\n\u00a0 \u00a0 putmail_dequeue.py<br>\n\u00a0 <span class=\"kw1\">fi<\/span><br><span class=\"kw1\">fi<\/span><\/div><\/div><\/pre>\n<p>After all that, I can use any mail client I wish and send mail with a standard SMTP call to <code>localhost:1025<\/code> and have it delivered next time I&#8217;m connected to the internet. And the best part is, I don&#8217;t have to change my workflow for email if I&#8217;m home or traveling \u2014 it all just happens automatically in the background.<\/p>\n<\/div>\n<div class=\"clearfix text-formatted field field--name-field-article-subhead field--type-text-long field--label-hidden field__item\">\n<p>Configure your mail client to automatically send emails next time you have an internet connection.<\/p>\n<\/div>\n<div class=\"field field--name-field-lead-image field--type-entity-reference field--label-hidden field__item\">\n<article class=\"media media--type-image media--view-mode-caption\">\n<div class=\"field field--name-field-media-image field--type-image field--label-hidden field__item\">  <img decoding=\"async\" src=\"https:\/\/www.cryptocabaret.com\/wp-content\/uploads\/2022\/05\/email_chat_communication_message.png\" width=\"1041\" height=\"584\" alt=\"Chat via email\" title=\"Chat via email\" loading=\"lazy\"><\/div>\n<\/article>\n<\/div>\n<div class=\"field field--name-field-tags field--type-entity-reference field--label-hidden field__items\">\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/tags\/email\" hreflang=\"en\">Email<\/a><\/div>\n<\/p><\/div>\n<div class=\"field field--name-field-listicle-title field--type-string field--label-hidden field__item\">What to read next<\/div>\n<div class=\"field field--name-field-listicles field--type-entity-reference field--label-hidden field__items\">\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/article\/22\/5\/gmailctl-linux-command-line-tool\" hreflang=\"en\">Manage your Gmail filters from the Linux command line<\/a><\/div>\n<\/p><\/div>\n<div class=\"field field--name-field-default-license field--type-list-string field--label-hidden field__item\"><a rel=\"license\" href=\"http:\/\/creativecommons.org\/licenses\/by-sa\/4.0\/\"><br \/>\n        <img decoding=\"async\" alt=\"Creative Commons License\" src=\"https:\/\/www.cryptocabaret.com\/wp-content\/uploads\/2022\/05\/cc-by-sa--24.png\" title=\"This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.\"><\/a>This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.<\/div>\n<section class=\"field field--name-field-comments field--type-comment field--label-hidden comment-wrapper\">\n<div class=\"comments__count\">\n<div class=\"login\"><a href=\"https:\/\/opensource.com\/user\/register?absolute=1\">Register<\/a> or <a href=\"https:\/\/opensource.com\/user\/login?current=\/rss.xml&amp;absolute=1\">Login<\/a> to post a comment.<\/div>\n<\/p><\/div>\n<\/section>\n<p class=\"wpematico_credit\"><small>Powered by <a href=\"http:\/\/www.wpematico.com\" target=\"_blank\" rel=\"noopener\">WPeMatico<\/a><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Travel off the grid and still send emails with putmail Kevin Sonney Tue, 05\/17\/2022 &#8211; 03:00 Register or Login to like Register or Login to like Automation is a hot topic right now. In my day job as an SRE part of my remit is to automate as many repeating tasks as possible. But how [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":64420,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[307],"tags":[],"class_list":["post-64419","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-open-source"],"_links":{"self":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts\/64419","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=64419"}],"version-history":[{"count":0,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts\/64419\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/media\/64420"}],"wp:attachment":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=64419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=64419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=64419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}