{"id":628,"date":"2011-11-07T15:28:06","date_gmt":"2011-11-07T22:28:06","guid":{"rendered":"http:\/\/www.michel2010.name\/blog\/?p=628"},"modified":"2011-11-07T15:28:06","modified_gmt":"2011-11-07T22:28:06","slug":"gmail-clone","status":"publish","type":"post","link":"https:\/\/blog.michel2010.name\/eric\/2011\/11\/07\/gmail-clone\/","title":{"rendered":"gmail Clone"},"content":{"rendered":"<p>Something I need to make\u00a0absolutely\u00a0clear from the start. \u00a0This post is <strong>NOT<\/strong> about setting up an actual email server. \u00a0This server sits on our local network, behind our router\/firewall. \u00a0It does not have direct access to the Internet. \u00a0As such I was not focusing on security and will not vouch for it (this setup) being secure.<\/p>\n<p>Ok, now that that is done, I need to give a little background. \u00a0Here at <a title=\"Beartooth Mountain Christian Ranch\" href=\"http:\/\/www.bmcrministries.org\" target=\"_blank\">BMCR<\/a>\u00a0we have\u00a0satellite\u00a0Internet. \u00a0This means we have very limited bandwidth. \u00a0In order to stay under our cap, I limit bandwidth per day by IP address. \u00a0Once you hit your daily limit you can&#8217;t get to the Internet till the next day. \u00a0That is all well and good, but if you hit your limit you can&#8217;t check your email. \u00a0This was starting to be a problem.<\/p>\n<p>Enter in these three programs <a title=\"Postfix\" href=\"http:\/\/www.postfix.org\/\" target=\"_blank\">postfix<\/a>, <a title=\"Dovecot\" href=\"http:\/\/www.dovecot.org\/\" target=\"_blank\">dovecot<\/a>, and <a title=\"Offlineimap\" href=\"https:\/\/github.com\/nicolas33\/offlineimap\" target=\"_blank\">offlineimap<\/a>. \u00a0With this setup, our server syncs itself with our Google Apps email accounts, and then we check and send our email on\/from the local server. \u00a0This way, even if you hit your bandwidth limit for the day, you can still do email (it also means that if for some strange reason all of Google&#8217;s servers die you have a local backup copy of all your emails). \u00a0I installed this on\u00a0<a title=\"Download Ubuntu Server\" href=\"http:\/\/www.ubuntu.com\/download\/ubuntu\/download\" target=\"_blank\">Ubuntu server 10.04<\/a>\u00a0(the 32bit version).<\/p>\n<p>I offer no\u00a0guarantees\u00a0that any of this will work for you, nor do I guarantee that it wont\u00a0completely\u00a0destroy your system if you try it.<\/p>\n<p>Sources:<\/p>\n<p style=\"padding-left: 30px;\">Postfix install and configuration: <a title=\"Postfix Installation and Configuration\" href=\"http:\/\/www.techienote.com\/2011\/01\/configuring-postfix-to-relay-email-through-gmail-in-ubuntu.html\" target=\"_blank\">Techienote.com<\/a><\/p>\n<p style=\"padding-left: 30px;\">Postfix setup for multiple smtp servers: <a title=\"Multiple SMTP Server Setup\" href=\"http:\/\/www.cyberciti.biz\/faq\/postfix-multiple-isp-accounts-smarthost-smtp-client\/\" target=\"_blank\">Cyberciti.biz<\/a>\u00a0(I don&#8217;t actually talk about this in this post, but I did set it up.)<\/p>\n<p style=\"padding-left: 30px;\">Dovecot install and configuration: <a title=\"Dovecot Install and Configuration\" href=\"http:\/\/www.webmonkey.com\/2010\/02\/set_up_a_debian_or_ubuntu_machine_as_a_maildrop\/\" target=\"_blank\">Webmonkey.com<\/a><\/p>\n<p style=\"padding-left: 30px;\">Offlineimap: <a href=\"http:\/\/www.offlineimap.org\" target=\"_blank\">Offlineimap.org<\/a><\/p>\n<p>&nbsp;<\/p>\n<h2>Postfix<\/h2>\n<h3>Install<\/h3>\n<p><code>sudo apt-get install postfix -y<\/code><\/p>\n<p style=\"padding-left: 30px;\">The &#8220;-y&#8221; matters, but I&#8217;m not sure why. \u00a0At the configuration screen select &#8220;No Configuration&#8221;<\/p>\n<p><code>sudo cp \/usr\/share\/postfix\/main.cf.debian \/etc\/postfix\/main.cf<\/code><\/p>\n<h3>Configuration<\/h3>\n<p><code>sudo vi \/etc\/postfix\/main.cf<\/code><\/p>\n<p>This is my main.cf:<\/p>\n<pre># See \/usr\/share\/postfix\/main.cf.dist for a commented, more complete version\n\n# Debian specific: \u00a0Specifying a file name will cause the first\n# line of that file to be used as the name. \u00a0The Debian default\n# is \/etc\/mailname.\n#myorigin = \/etc\/mailname\n\nsmtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)\nbiff = no\n\n# appending .domain is the MUA's job.\nappend_dot_mydomain = no\n\n# Uncomment the next line to generate \"delayed mail\" warnings\n#delay_warning_time = 4h\n\nreadme_directory = no\n\n# TLS parameters\nsmtpd_tls_cert_file=\/etc\/ssl\/certs\/ssl-cert-snakeoil.pem\nsmtpd_tls_key_file=\/etc\/sll\/private\/ssl-cert-snakeoil.key\nsmtpd_use_tls=no\nsmtpd_tls_session_cache_database = btree:${data_directory}\/smtpd_scache\nsmtp_tls_session_cache_database = btree:${data_directory}\/smtp_scache\n\n# See \/usr\/share\/doc\/postfix\/TLS_README.gz in the postfix-doc package for\n# information on enabling SSL in the smtp client\n\nmyhostname = repository.bmcr\nalias_maps = hash:\/etc\/aliases\nalias_database = hash:\/etc\/aliases\nmyorigin = \/etc\/mailname\nmydestination =\nrelayhost = [smtp.gmail.com]:submission\nmynetworks = 192.168.1.0\/24, 127.0.0.0\/8\nmailbox_size_limit = 0\nrecipient_delimiter = +\ndefault_transport = smtp\nrelay_transport = smtp\ninet_protocols = all\nhome_mailbox = email\/\nmailbox_command = \n\n# SASL Settings\nsmtp_use_tls=yes\nsmtp_sasl_auth_enable = yes\nsmtp_sasl_password_maps = hash:\/etc\/postfix\/sasl_passwd\nsmtp_sasl_security_options = noanonymous\nsmtp_sasl_tls_security_options = noanonymous\nsmtp_tls_CAfile = \/etc\/postfix\/cacert.pem\nsmtp_sender_dependent_authentication = yes\nsender_dependent_relayhost_maps = hash:\/etc\/postfix\/relayhost_map\nsmtp_generic_maps = hash:\/etc\/postfix\/generic<\/pre>\n<p>Just a note, the &#8220;home_mailbox&#8221;\u00a0parameter. \u00a0On our server every ones email is in a folder called &#8220;email&#8221; in their home directory. \u00a0You can call that folder whatever you want.<\/p>\n<p><code>sudo echo \"myfully.qualifieddomain.name\" &gt; \/etc\/mailname<\/code><\/p>\n<p><code>sudo vi \/etc\/postfix\/sasl_passwd<\/code><\/p>\n<p>This opens the postfix password file. \u00a0One username and password per line.<\/p>\n<p>For example:<\/p>\n<pre>#Per-sender authentication\nusername@Repository.BMCR username@bmcrministries.org:password\n#Default login\n[smtp.gmail.com]:587 username@bmcrministries.org:password<\/pre>\n<p>I&#8217;ll try and explain the following commands, but the sources links will have better\u00a0explanations\u00a0(owing to the fact they know what they are doing).<\/p>\n<p>This secures the password file (the passwords are stored in it plain text):<\/p>\n<p><code>sudo chmod 400 \/etc\/postfix\/sasl_passwd<\/code><\/p>\n<p>Postfix keeps some kind of a database hash of the postfix password file and the mailname file. \u00a0These update the databases after a change (these must be run whenever you modify there respective files):<\/p>\n<p><code>sudo postmap \/etc\/postfix\/sasl_passwd<\/code><br \/>\n<code>sudo newaliases<\/code> (this is for the \/etc\/mailname file&#8230; I think)<\/p>\n<p>Setup the CA certificate. \u00a0This is a self-signed certificate, so most email clients\/browsers will throw up a warning. \u00a0Remember this is running on our local network and isn&#8217;t\u00a0accessible\u00a0from outside.<\/p>\n<p><code>sudo touch \/etc\/postfix\/cacert.pem<\/code><\/p>\n<p><code>sudo cat \/etc\/ssl\/certs\/Thawte_Premium_Server_CA.pem &gt;&gt; \/etc\/postfix\/cacert.pem<\/code><\/p>\n<p>Now restart postfix.<\/p>\n<p><code>sudo \/etc\/init.d\/postfix restart<\/code><\/p>\n<p>To test postfix:<\/p>\n<p><code>sudo apt-get install mailutils<\/code> This will let you send an email from the command line.<br \/>\n<code>echo \u201cWe rolling, yo\u201d | mail -s \u201cSent from Repository\u201d someone@somewhere.com<\/code><\/p>\n<p>If you&#8217;ve got everything configured properly this should send an email. \u00a0The subject line will be the text following the &#8220;-s&#8221; and the body will be the text following &#8220;echo&#8221;. \u00a0The address is that of the\u00a0recipient.<\/p>\n<p>A couple of notes:<\/p>\n<p style=\"padding-left: 30px;\">I had &#8216;inet_interfaces&#8217; explicitly stated in my main.cf. For some reason that didn&#8217;t work. I removed the line and it defaulted properly. Once that was done I started getting alias.db errors, which were fixed by &#8216;sudo newaliases&#8217;. That was some how tied to &#8216;mynetworks&#8217;. That should default to the local subnet and local host, however I had to explicitly put that in. Once these things were figured out I could send email using Thunderbird.<\/p>\n<h2>Dovecot (IMAP server)<\/h2>\n<h3>\u00a0Install<\/h3>\n<p><code>sudo apt-get install dovecot-imapd<\/code><\/p>\n<h3>Configuration<\/h3>\n<p><code>sudo nano \/etc\/dovecot\/dovecot.conf<\/code><\/p>\n<p>Find &#8220;protocols&#8221; and set it to &#8220;protocols = imap imaps&#8221;<\/p>\n<p>Find &#8220;mail_location&#8221; and set that to &#8220;mail_location = maildir:\/home\/%u\/email:LAYOUT=fs&#8221; (if you are using a different folder name make sure to use that instead)<\/p>\n<h2>Offlineimap<\/h2>\n<h3>Install<\/h3>\n<p>For whatever reason I couldn&#8217;t get the Offlineimap in the Ubuntu repositories to work, so I installed from a different repository.<\/p>\n<p>In order to add a ppa run this command:<\/p>\n<p><code>sudo apt-get install python-software-properties<\/code><\/p>\n<p>Then add the ppa:<\/p>\n<p><code>sudo add-apt-repository ppa:pomeo\/offlineimap<\/code><\/p>\n<p>Then of course:<\/p>\n<p><code>sudo apt-get update<\/code><br \/>\n<code>sudo apt-get install offlineimap<\/code><\/p>\n<p>Offlineimap is configured on a per user basis. \u00a0Each user will have an .offlineimaprc file in their home directory. OfflineIMAP is very picky about the configuration file. If you are getting errors open the file up again and double check your settings. For example if you have a space before \u201cui = TTY.TTYUI\u201d it will error out.<\/p>\n<p>My offlineimaprc:<\/p>\n<pre>[general]\naccounts = BMCR\n\nui = TTY.TTYUI\n\n[Account BMCR]\n#Profile for local mail\nlocalrepository = localrep\n#Profile for remote mail\nremoterepository = remoterep\n\n[Repository localrep]\ntype = Maildir\nlocalfolders = ~\/email\/\n\n[Repository remoterep]\ntype = IMAP\nremotehost = imap.gmail.com\nssl = yes\nremoteuser = someone@somewhere.com\nremotepass = password\nnametrans = lambda folder: re.sub('.*Drafts$', 'Drafts', re.sub('.*Sent Mail$', 'Sent_Mail', re.sub('.*Trash$', 'Trash', folder)))\nfolderfilter = lambda foldername: foldername not in ['[Gmail]\/All Mail','[Gmail]\/Spam','[Gmail]\/Starred','[Gmail]\/Important']\nmaxconnections = 2<\/pre>\n<p>I would also chmod this to 400 when your done as it also contains a plain text password. \u00a0The documentation at <a href=\"http:\/\/offlineimap.org\/\" target=\"_blank\">offlineimap.org<\/a> should walk you through all the options I&#8217;ve used.<\/p>\n<p>I hope this was\u00a0useful. \u00a0If something is un-clear, or simply wrong please let me know and I&#8217;ll try and fix it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Something I need to make\u00a0absolutely\u00a0clear from the start. \u00a0This post is NOT about setting up an actual email server. \u00a0This server sits on our local network, behind our router\/firewall. \u00a0It does not have direct access to the Internet. \u00a0As such I was not focusing on security and will not vouch for it (this setup) being &hellip; <a href=\"https:\/\/blog.michel2010.name\/eric\/2011\/11\/07\/gmail-clone\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;gmail Clone&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-628","post","type-post","status-publish","format-standard","hentry","category-tech"],"_links":{"self":[{"href":"https:\/\/blog.michel2010.name\/eric\/wp-json\/wp\/v2\/posts\/628","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.michel2010.name\/eric\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.michel2010.name\/eric\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.michel2010.name\/eric\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.michel2010.name\/eric\/wp-json\/wp\/v2\/comments?post=628"}],"version-history":[{"count":0,"href":"https:\/\/blog.michel2010.name\/eric\/wp-json\/wp\/v2\/posts\/628\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.michel2010.name\/eric\/wp-json\/wp\/v2\/media?parent=628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.michel2010.name\/eric\/wp-json\/wp\/v2\/categories?post=628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.michel2010.name\/eric\/wp-json\/wp\/v2\/tags?post=628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}