6

I have a lot emails in my mail box with some specific label like "NR-Support" which contains emails from www.naveedramzan.com contact form.

I have developed ticketing system and now its directly saving ticketing system from Contact form.

But, I want to transform all old emails marked as NR-Support to that ticketing system.

I have tried with imap_open but didn't get any clue to get emails of specific label.

$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'naveed.ramzan@gmail.com';
$password = 'abc123';
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
$emails = imap_search($inbox,'ALL');
if($emails) {
    $output = '';
    rsort($emails);
    foreach($emails as $email_number) {
        $overview = imap_fetch_overview($inbox,$email_number,0);
        $message = imap_fetchbody($inbox,$email_number,2);
        $output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">';
        $output.= '<span class="subject">'.$overview[0]->subject.'</span> ';
        $output.= '<span class="from">'.$overview[0]->from.'</span>';
        $output.= '<span class="date">on '.$overview[0]->date.'</span>';
        $output.= '</div>';
        $output.= '<div class="body">'.$message.'</div>';
    }
    echo $output;
}
imap_close($inbox);
Naveed Ramzan
  • 3,565
  • 3
  • 25
  • 30
  • I don't think I should have to say this to somebody with rep ~500, but please provide your code and what you've actually tried. Also, if it's your contact form, why can't you push the ticket in the same place as where you send the e-mail? – Jonnix Jan 15 '16 at 10:02
  • Jon, I have updated my code which submits ticket on contact form. But I want to shift my old emails which I received (when I haven't ticketing system) – Naveed Ramzan Jan 15 '16 at 10:04
  • Naveed, that makes sense. If you can show your working then people will have a better idea of what to suggest. – Jonnix Jan 15 '16 at 10:08
  • 2
    After some quick checking, it's probably worth trying changing the `INBOX` part of `$hostname` to point at the relevant imap "folder" (gmail labels). Unfortunately I'm not sure what you'd need to change it _to_ :S. – Jonnix Jan 15 '16 at 11:12
  • OK I will try that and then update here. – Naveed Ramzan Jan 15 '16 at 12:50
  • Thanks @JonStirling, it worked and I have posted answer. – Naveed Ramzan Jan 18 '16 at 06:15

1 Answers1

7

For accessing specific label in mail box, we need to specific the label here.

$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';

$hostname = '{imap.gmail.com:993/imap/ssl}LABEL';
Naveed Ramzan
  • 3,565
  • 3
  • 25
  • 30