Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
Reading and using emails including enclosures on your web server.

The imap module ships with PHP, but isn't always built into the server; you need to build PHP with --with-imap if it's not there. But once you do have the module , you can access your POP3 email server direct from PHP.

Very recently, I was asked if I could extract attachments / enclosures from emails sent to the web server, and save them there into a database, with the attach,ents and enclosures in a usable form. Such emails are typically multipart, and I like a challenge. But it turns out to be fairly straightforward once the imap module is loaded and you understand how it works.

1. imap_open established a connection to the IMAP server (the question asked about this being the web server, but it could be any computer that's a mial seever)

2. imap_headers finds out what's in the mailbox for you.

3. A loop of imap_fetchstuctures gets details of each email and within that

4. A loop of imap_fetchbodys looks at the main and (if present) subparts of each message.

Functions such as imap_base64 and imap_qprint may be used to convert encoded content back to the original enclose, and imap_fetchmime or the type and subtype fields may be used to identify content type, thus disposition.

Full source code of the demo is [here]. Saving to a database rather than a plain text file is "trivial" ... or at least beyond the scope of this demo, and something that's covered here in many places already. The picture illustrating this item show sample output, including an image that the PHP has extracted from email.

My original questioner was looking for a handler that wasn't in fact interactive - that took emails as they arrived and saved them away to server files. That could be done through a PHP script running via crontab rather than via a webserver, or the whole job could be silently run from time to time within a regular user's request, in the same way that PHP cleans up abandoned sessions. There is a VERY old example of this [here] - from before we took the lazy practical approach of using PHP's new (at the time) $_SESSION array.


Footnotes -

1. I should warn you to think carefully about the security issues involved in fishing stuff out of emails and making automatic use of it on your server

2. The imap module also allows you to delete and send emails, mark them as read, etc - so you can write yourself a full webmail client if you want.
(written 2011-09-23)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
H201 - PHP and MySQL for Content Management
  [2071] Setting up a MySQL database from PHP - (2009-03-08)
  [1745] Moodle, Drupal, Django (and Rails) - (2008-08-08)
  [1576] Making PHP and MySQL training relevant to the course delegates - (2008-03-15)
  [1555] Advanced Python, Perl, PHP and Tcl training courses / classes - (2008-02-25)
  [647] Checking for MySQL errors - (2006-03-15)

H401 - Some extra PHP modules
  [3454] Your PHP website - how to factor and refactor to reduce growing pains - (2011-09-24)
  [2682] Adding extensions to PHP Open Source applications - callbacks - (2010-03-17)
  [2343] World Flags in your PHP pages - (2009-08-10)
  [732] Where is a web site visitor browsing from - (2006-05-24)


Back to
Which or ATOC - who reads train fares right?
Previous and next
or
Horse's mouth home
Forward to
Your PHP website - how to factor and refactor to reduce growing pains
Some other Articles
Away to train - but still around by video for Melksham meetings
Stepping stones - early coding, and writing re-usable code quickly
MySQL, MySQLi, PDO or something else - how best to talk to databases from PHP
Reading and using emails including enclosures on your web server.
Which or ATOC - who reads train fares right?
Why would you want to use a Perl hash?
A threat in the post? Poor marketing practise from Smiletrain?
Apache Internal Dummy Connection - what is it and what should I do with it?
Checking all the systems on a subnet, using Expect and Tk
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3453_Rea ... rver-.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat