I'm using these libraries for an email application I'm building.

$storage = new Zend_Mail_Storage_Imap($imap);

if (strpos($storage->getMessage($i),'chocolate') !== FALSE ) 

{
        //Move to chocolate folder here
}

Also, is there a way to put incoming emails from this sender to automatically go into the chocolate folder from now on? ...similar to how you can build labels in gmail and all mail from that sender will automatically go into that folder in the future.

Dani AI

Generated

is correct that the storage adapter can move messages, but for Gmail accounts you probably want server-side filters so routing happens even when your app is not running.

Zend IMAP notes
Zend_Mail's IMAP adapter exposes a writable API (move/copy/remove). The adapter emulates a "move" with COPY + delete when the IMAP server does not support the MOVE extension. (See the adapter docs.)
(https://www.magentoextensions.org/documentation/class_zend___mail___storage___imap.html)

Use Gmail filters (recommended for Gmail/OAuth users)
If users authenticate with Google, create a Gmail label and then create a Gmail filter via the Gmail API; filters run on Google’s servers and will automatically apply to future mail. The REST endpoint is users.settings.filters.create and accepts a criteria + action (for example: match from and addLabelIds + removeLabelIds to archive). The filter-creation scope is https://www.googleapis.com/auth/gmail.settings.basic.
(Gmail filters API reference: https://developers.google.com/workspace/gmail/api/reference/rest/v1/users.settings.filters/create)
(Guide: https://developers.google.com/workspace/gmail/api/guides/filter_settings)

Quick examples (cURL)
Create a label, then create a filter that labels and archives incoming mail from a sender:

# create label
curl -X POST -H "Authorization: Bearer $ACCESS_TOKEN" -H "Content-Type: application/json" \
  -d '{"name":"chocolate"}' \
  "https://gmail.googleapis.com/gmail/v1/users/me/labels"

# create filter (replace LABEL_ID)
curl -X POST -H "Authorization: Bearer $ACCESS_TOKEN" -H "Content-Type: application/json" \
  -d '{"criteria":{"from":"sender@example.com"},"action":{"addLabelIds":["LABEL_ID"],"removeLabelIds":["INBOX"]}}' \
  "https://gmail.googleapis.com/gmail/v1/users/me/settings/filters"

Apply to existing mail
Filters affect future messages only. To move/label existing messages, use messages.modify or messages.batchModify to add/remove labels programmatically.
(Modify/batchModify docs: https://developers.google.com/gmail/api/reference/rest/v1/users.messages/modify and https://developers.google.com/gmail/api/reference/rest/v1/users.messages/batchModify)

Notes and cautions

  • Filters have limits (e.g., max ~1,000 filters per account). See the filters guide for details.
  • Only one user-defined label may be added per filter action; use batchModify to tag multiple messages if needed.
  • Creating filters and labels requires proper OAuth scopes (gmail.settings.basic, and gmail.modify or mail.google.com for message edits).
    If you need a simple, server-side rule engine for non-Gmail IMAP accounts, run a periodic worker that scans inboxes and calls the storage adapter to move messages.

Recommended Answers

All 2 Replies

You want to use the $storage->moveMessage( id, folder) method.
This site illustrates it a little more in detail:

The comments at the bottom of the Zend_Mail documentation also elaborate on this: http://framework.zend.com/manual/en/zend.mail.read.html

Automatically routing an email from a user to a particular folder would require some kind of intermediate logic that would look at all the emails and move them according to some kind of rule. Using a database or some kind of storage would make this possible.

Thanks for the advice and the links. I'm actually using google oauth for use with this application and I've seen gmail filters (although not with my existing application ) for folders. Is there a way to tap into Google's logic since all our users will be using their platform to access their mail?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.