{"id":1617,"date":"2014-06-13T16:14:55","date_gmt":"2014-06-13T16:14:55","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=1617"},"modified":"2014-09-26T15:05:12","modified_gmt":"2014-09-26T15:05:12","slug":"fetchmail-to-receive-email","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/software_utilities\/email\/fetchmail-to-receive-email","title":{"rendered":"fetchmail to receive email"},"content":{"rendered":"<p>\nfetchmail is a full-featured IMAP and POP client\n<\/p>\n<h4>\nInstall<br \/>\n<\/h4>\n<pre>\r\n<code>\r\nsudo apt-get install fetchmail\r\n<\/code><\/pre>\n<h4>\nConfigure How Mail Is Handled<br \/>\n<\/h4>\n<p>\nNow create a file named ~\/.fetchmailrc in your home directory.\n<\/p>\n<pre>\r\n<code>\r\nsudo nano\r\n<\/code><\/pre>\n<p>\nEnter this:\n<\/p>\n<pre>\r\n<code>\r\npoll imap.gmail.com\r\nprotocol IMAP\r\nuser &quot;AGmailUser@gmail.com&quot; with password &quot;TheGmailPassword&quot; mda &quot;\/home\/pi\/myfetchmailparser.sh&quot;\r\nfolder &#39;INBOX&#39;\r\nfetchlimit 1\r\n#keep\r\nssl\r\n<\/code><\/pre>\n<p>\nFetchmail by default will try and pass mail to port 25 on localhost. &nbsp;Using&nbsp;mda&nbsp;(mail delivery agent) tells it to pass it to the mda instead, which in this case is a script we will create.\n<\/p>\n<p>\nIf you don&#39;t want the email to be deleted from the mailbox remove the # commenting out the keep line (this doesn&#39;t mean the email will be read again though when you next run the script, it will simply remains in the inbox marked as read. &nbsp;However if you go to the mailbox via say gmails web interface and mark the email as unread again then it will be included the next time you call the script &#8211; useful for testing)\n<\/p>\n<p>\nPress CTRL+X to exit the file, select save and enter:\n<\/p>\n<pre>\r\n<code>\r\n\/home\/pi\/.fetchmailrc\r\n<\/code><\/pre>\n<p>\nNow we need to create the script which we&#39;ll set up to simply write received emails out to a text file each.\n<\/p>\n<pre>\r\n<code>\r\nsudo nano\r\n<\/code><\/pre>\n<p>\nEnter this:\n<\/p>\n<pre>\r\n<code>\r\nFilenameUniqueId=$(date +&quot;%Y%m%d_%H%M%S_%N&quot;)\r\nOutputFile=&quot;\/var\/tmp\/mail&quot;$FilenameUniqueId\r\necho &quot;&quot; &gt; $OutputFile\r\nwhile read x\r\ndo \r\n#echo $x\r\necho $x &gt;&gt; $OutputFile\r\ndone\r\n<\/code><\/pre>\n<p>\nPress CTRL+X to exit the file, select save and enter:\n<\/p>\n<pre>\r\n<code>\r\n\/home\/pi\/myfetchmailparser.sh\r\n<\/code><\/pre>\n<h5>\nSet Permissions<br \/>\n<\/h5>\n<p>\nFetchmail is very particular about permissions.\n<\/p>\n<p>\nSet the current user as the owner (only the current user can run .fetchmailrc):\n<\/p>\n<pre>\r\n<code>\r\nsudo chown pi .fetchmailrc\r\nsudo chown pi myfetchmailparser.sh\r\n<\/code><\/pre>\n<p>\nSet the files permissions\n<\/p>\n<pre>\r\n<code>\r\nsudo chmod 710 .fetchmail.rc\r\nsudo chmod 710 myfetchmailparser.sh\r\n<\/code><\/pre>\n<p>\n(these may not be optimum settings &#8211; we just wanted to get it working and this worked!)\n<\/p>\n<h5>\nRunning It<br \/>\n<\/h5>\n<pre>\r\n<code>\r\nfetchmail &gt; \/dev\/null\r\n<\/code><\/pre>\n<p>\nNow look in the&nbsp;\/var\/tmp\/ folder and you should see the new file which will&nbsp;contain the email text.\n<\/p>\n<p>\nIf you need to do this from inside a C application which is running as root (e.g. an application&nbsp;using the IO pins) you can call it like this to make the call as the &quot;pi&quot; user instead of root (which won&#39;t work)\n<\/p>\n<pre>\r\n<code>\r\n\tsystem(&quot;su - pi -c \\&quot;fetchmail &gt; \/dev\/null\\&quot;&quot;);\r\n<\/code><\/pre>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>fetchmail is a full-featured IMAP and POP client Install sudo apt-get install fetchmail Configure How Mail Is Handled Now create a file named ~\/.fetchmailrc in your home directory. sudo nano Enter this: poll imap.gmail.com protocol IMAP user &quot;AGmailUser@gmail.com&quot; with password &quot;TheGmailPassword&quot; mda &quot;\/home\/pi\/myfetchmailparser.sh&quot; folder &#39;INBOX&#39; fetchlimit 1 #keep ssl Fetchmail by default will try and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[88],"tags":[],"class_list":["post-1617","post","type-post","status-publish","format-standard","hentry","category-email"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/1617","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=1617"}],"version-history":[{"count":13,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/1617\/revisions"}],"predecessor-version":[{"id":1642,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/1617\/revisions\/1642"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=1617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=1617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=1617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}