Search code examples
phpemailimap

How do I copy mail into another mailbox using imap_mail_copy?


The usually comprehensive PHP manual is missing any complete examples of using the imap_mail_copy function. Specifically I'm looking for an example of what to supply in the $msglist variable to the function. The documentation recommends reading RFC2060 which I cannot comprehend.

I want to ultimately write a function to copy a message into the a Sent folder upon sending an email.

Can someone provide an example of using the imap_mail_copy function?


Solution

  • If you're just copying one message, use the same msg_number you'd use in an imap_headerinfo call (etc.).

    If you're copying more than one message, join the message numbers with commas (but no spaces). E.g. "3,4,7,9".

    The lousy documentation for imap_mail_copy says:

    msglist is a range not just message numbers (as described in » RFC2060).

    What this means is that if you want to copy a bunch of messages with consecutive message numbers (e.g. 1,2,3,4,5,6,7) you can just use a "range" and encode it as "1:7". But, honestly, it's usually much easier to just have a comma-separated list.

    It's described a lot better in the imap_fetch_overview page:

    A message sequence description. You can enumerate desired messages with the X,Y syntax, or retrieve all messages within an interval with the X:Y syntax